Dailycode.info

Short solution for short problems

Using webservices in a webpart

I'm developing webparts not on the server, but a simple develop machine. It is not possible to use the visual studio Web part extensions. So I create and copy the whole thing myself.

No big problems there. But then come the choice, adding the assembly to the GAC or not? I choose not to, because of security reasons. I just copy my DLL to the bin folder of the virtual directory. Works fine, I did had to put the web.config to medium trust and add this line to the web.config:

<SafeControl Assembly="SOP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=93464d75f4108077" 
Namespace="MYNAMESPACE" TypeName="*" Safe="True" AllowRemoteDesigner="True" />
 

Don't forget to add the webpart, you can do this when you choose site settings and click on webparts.

When I started working with webservices in the webpart, it became a lillte more difficult. But thanks to google and some collegue programmers I solved this lovely exception:

System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, 
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
 

This site explains it good: http://store.bamboosolutions.com/kb/article.aspx?id=10405 

I extracted the steps you'll have to take in order to clear up this error:

You'll have to add these lines to the medium trust file (or a custom trust file): 

<IPermission class="System.Net.WebPermission, System, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1"> 
<ConnectAccess>  
<URI uri="http://myserver:myport/mysitecollection\mywebservice.asmx"/
</ConnectAccess>
</IPermission>
 

The path of the trust files can be found in the web.config. In my case it was:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG
 

You can add multiple url's!