How do you structure your WCF projects?

WCF projects do not always tend to stay in one project solution. As the business requirement grows, we see that we might need to break up our WCF solution into various bits. Here is a way I handle my WCF projects.

I break up my WCF project into the following bits (each being a class library in Visual Studio):

  • DataContracts
  • MessageContracts
  • FaultContracts
  • ServiceContracts
  • ServiceImplementation
  • ServiceHost

The ServiceHost project can be a self hosted application or a WCF Service Application hosted in IIS. This can change according to your business requirements. This breakup helps me to handle each of the component more efficiently.

image

There is still one thing that we need to address here – How do we plan to handle Service Errors(and possibly return them back to the Client)? Again, there are many ways to accomplish this, but best is to go with your business needs.I will be writing more about this soon.

I am very much interested to know how you handle your WCF projects and the structure you use for WCF projects. Feel free to share your thoughts

WCF and Sharepoint - System.IO.FileNotFound Exception

I am currently building a WCF service that has some Sharepoint code in it. The service uses wsHttpBinding. The service is hosted in Windows Server 2003 running IIS 6 with integrated windows authentication mode.

It was really strange to me when the service threw this (really a weird) exception:

The Web application at http://localhost/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application

The code which was throwing this exception was none other than:

   1: using(
   2:   SPSite hostSite = 
   3:         new SPSite(request.SiteCollectionUrl))
   4: { ... }

Searching the web led me here

The solution was to set the the WCF Service's application pool's Identity to the same account Sharepoint website's application pool uses. Do not try to set the application pool to the same of Sharepoint as it might bring down the Sharepoint website when something goes wrong with the service!

app-pool

How to verify this?

Good question :)

If you open up your WCF client configuration file, look for the <identity> tag inside the Endpoint configuration

   1: <identity>
   2:    <userPrincipalName 
   3:         value="mossservicedev@BUILD.DEV" />
   4: </identity>

You can see that the identity is set to an account in the domain. Verify that with your application pool in IIS

app-pool-identity

If they both look the same, your Sharepoint code in the web service should work :)


Creative Commons License
Chaks' Corner Blog by Chakkaradeep Chandran is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Based on a work at www.chakkaradeep.com.
Permissions beyond the scope of this license may be available at http://www.chakkaradeep.com.