Featured Post

SQL Query in SharePoint

The "FullTextSqlQuery" object's constructor requires an object that has context but don't be fooled. This context will no...

Friday, September 9, 2011

Create a custom WCF NET.TCP service

I recently learned about creating a custom net.tcp service. I thought I'd make a nice step by step post on how to do this. Also, I'd just to note that this a standard WCF service example hosted in IIS 7.

Ok, so this is how to create a super simple net.tcp service

1.
Firstly, create a server application. In this example my server is a simple class library project. Now the only thing you'll need in this project are 4 files (see image). You will also need to add a reference to System.ServiceModel.











 2. UploaderService.svc 

This file needs to contain your servicehost tag. Set the "Service" value to that of your service class (including the namespace) and if you're deploying to the gac, set the assembly information otherwise. In this example I'm going to use CodeBehind. Here is an example of what your svc file should look like:


3. IUploaderService.cs

Add the ServiceContract attribute to your interface and make sure that all methods have the OperationContract attribute as well.

 4. UploaderService.svc.cs

This is your service class. Nothing fancy needs to happen here, just make sure your service implements your interface and you add all required methods. I added a Ping method that returns a simple boolean to help during setting up and debugging.



5. Web.Config

You can either apply settings to your service via code or via configuration settings. In this example I will demonstrate via the configuration settings. There are thousands of settings you can use but I am going to show you the most basic example you will ever need just to get your service working.


so you could literally copy this exact xml doc and just change the address for the service to wherever you plan on deploying it.

6. IIS Setup

So that's the server side code. To deploy the code, drop your svc, config and dll (in a bin folder) into a folder and add it as a virtual directory in a website. 


Last step for your service is to add the net.tcp as an enabled protocol and setup your bindings for your virtual directory. See images below for settings:


Now check whether your service is pingable via http.


7. Calling your service from the client application.

You can use one of the service utils to generate some client code to use in your client application but mine isn't working so I just copied the interface from my server to my client (if the interface ever changes you'd have to regenerate a new client file anyways so might as well just copy paste). I then use the interface to create a System.ServiceMode.ChannelFactory instance pointing to my service. Here is a code example:

Your client configuration will look similar to the server except that you won't need the service behaviour settings and instead, add the .net version type in the supportedRuntime setting.

 
Run your application and see if you can ping your service.

8. Debugging

If you receive any errors, you can add diagnostics to your config file that will give you more detailed information about the exception that is occurring between the client and the server. Add the following xml to your client application which will output a file in your bin which you can open using the svc tools.



9. Tips

So I made a very silly mistake this week. My web application was set to .NET 2 and my client was set to .NET 4. This obviously did not work so please make sure that the application pool that you are running your site under has the same .NET version as your client application.

Secondly my service's platform was set to 32 bit and my client's was 64 bit. This was also a problem so make sure that they are both built for the same platform.

10. The End

I hope this post helps you. If there is anything I might have missed please let me know and happy net.tcp.ing :)


SharePoint 2010 COM SaveBinaryDirect not reliable

I've recently found that the SharePoint 2010 client object model is not reliable. Calling the SaveBinaryDirect method was working fine for weeks until it started throwing errors. I looked up these errors and found no help. One of the errors I was receiving was "The remote server returned an error: (409) Conflict." Because I was unable to find any information about this error (that was relevant - there was no conflict), we decided to rather create a custom net.tcp service to handle the bulk document uploading.