Installing SmartPart for Sharepoint (with a dash of WCF) 2 of 2

In my last post I explained (as countless other blogs have) how to install SmartPart on SharePoint and get a user control to display inside it. Now I'm going to try and give you a few pointers on some of the pitfalls involved in trying to get your WebPart to call a WCF service (short and sweet really).

First and rather obviously, move the system.serviceModel element under the configuration root element in your website's web.config. This means your webpart will have access to the binding and endpoint information when you make the service call.

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IRegistrationService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
            <message clientCredentialType="UserName" algorithmSuite="Default"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://localhost:8732/Design_Time_Addresses/POC/RegistrationService/" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRegistrationService" contract="RegistrationService.IRegistrationService" name="BasicHttpBinding_IRegistrationService"/>
    </client>
  </system.serviceModel>
 

When I tried calling the service from inside my user control (wrapped up inside the SmartPart) I got a wierd exception telling me 'Request for the permission of type 'System.Net.WebPermission' failed', something to do with permissions my keen developer instinct told me.

It turns out that your SharePoint's default web.config stipulates only minimal trust for all user assemblies executing within its context (this is controlled by the CLR's CAS security block). To fix it, i set my website's trust level to 'Full'.

    <trust level="Full" originUrl="" />

This is obviously not reccommended for blatantly obvious security reasons but for my test purposes on my personal little sharepoint box it did the job! Now I can call WCF services in glorious technicolour.

Hope that helps.