Sunday, January 13, 2013

Silverlight 4, subclassing WebClient

Silverlight 4, subclassing WebClient

Following an advice, I saw at several web pages (for example C#: Using CookieContainer with WebClient class), I subclassed WebClient class to use a cookie with it:

public class MyWebClient : System.Net.WebClient {  } 

Now, when I initialize MyWebClient:

MyWebClient wc = new MyWebClient(); 

it throws TypeLoadException. My OS is Windows 7 (japanese), so error message is not in english; I see it is related to security rules. What might be the problem?

Answers & Comments...

Answer: 1

WebClient's constructor is marked with the SecuritySafeCritical attribute. And it looks like that is what is causing the security exception. I tried applying that same attribute to MyWebClient's constructor but that didn't work. From what I've read, this kind of thing just isn't allowed in Silverlight. For example, see this other question.

For reference, the exact exception message is:

System.TypeLoadException

Inheritance security rules violated while overriding member: 'MyWebClient..ctor()'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

I wish there was a better answer...

by : Stephen McDanielhttp://stackoverflow.com/users/385996

Answer: 2

You need to implement a default constructor with the SecuritySafeCritical attribute. Had this problem today and that was the solution.

public class MyWebClient : System.Net.WebClient {     [SecuritySafeCritical]     public MyWebClient() : base() {} } 
by : ianthetechiehttp://stackoverflow.com/users/943029




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog