Inheritance security rules violated while overriding member in Silverlight -
i working on web application in silverlight. have overloaded webclient.getwebrequest method given below:-
public class webclientwithcookies : webclient { [securitycritical] protected override webrequest getwebrequest(uri address) { string cookiecontent = htmlpage.document.cookies; webrequest request = base.getwebrequest(address); httpwebrequest webrequest = request httpwebrequest; if (webrequest != null && cookiecontent != null && cookiecontent != string.empty) { cookiecontainer cookiecontainer = new cookiecontainer(); cookiecontainer.add(address, new cookie() { value = htmlpage.document.cookies }); webrequest.cookiecontainer = cookiecontainer; } return request; } } but getting following exception:
system.typeinitializationexception unhandled user code
message=the type initializer 'sigmawc.utility.restcommunicator' threw exception. typename=sigmawc.utility.restcommunicator
stacktrace: @ sigmawc.utility.restcommunicator..ctor() @ sigmawc.app..ctor() innerexception: system.typeloadexception message=inheritance security rules violated while overriding member: 'sigmawc.utility.webclientwithcookies..ctor()'. security accessibility of overriding method must match security accessibility of method being overriden. stacktrace: @ sigmawc.utility.restcommunicator..cctor() innerexception:
can in how elevate security settings in silverlight.
documentation scarce least. however, there couple of resources useful:
msdn indicates cannot use framework members securitycriticalattribute.
types , members have securitycriticalattribute cannot used silverlight application code. security-critical types , members can used trusted code in .net framework silverlight class library.
in case of webclient, getwebrequest method not have attribute, constructor does.
this msdn security blog implies if default constructor has security attribute, class cannot used inheritance in silverlight client.
further that, aforementioned msdn blog implies security attributes ignored in silverlight assemblies not part of core framework. may apply assembly level attributes.
anyway, cut long story short. you cannot derive webclient because of securitysafeattribute on constructor. illustrate point, causes exception @ runtime:
public class mywebclient : webclient { } the alternative roll own webclient. takes little work, following example work following handler:
public class myhandler : ihttphandler { public void processrequest(httpcontext context) { context.response.contenttype = "text/plain"; context.response.write("hello world"); foreach (cookie cookie in context.response.cookies) { //cookies client - there 1 in case } } ...
public class mywebclient { public mywebclient() { } public void invokewebrequest(uri address) { //set cookie want use. string cookiecontent = "i cookies"; // register http client - without following webrequest.cookiecontainer setter throw exception webrequest.registerprefix("http://", webrequestcreator.clienthttp); //this bit know, dont forget set name on new cookie. httpwebrequest webrequest = webrequest.create(address.absoluteuri) httpwebrequest; if (webrequest != null && !string.isnullorwhitespace(cookiecontent)) { webrequest.cookiecontainer = new cookiecontainer(); webrequest.cookiecontainer.add(address, new cookie() { value = cookiecontent, name = "mycookie" }); } //invoke async getresponse method. webrequest.begingetresponse(o => { httpwebresponse response = (httpwebresponse)webrequest.endgetresponse(o); using (streamreader reader = new streamreader(response.getresponsestream())) { //read result string result = reader.readtoend(); } foreach (cookie cookie in response.cookies) { //the cookies returned server. } }, null); } }
Comments
Post a Comment