Create and host WCF service as windows service in C#.Net

This WCF tutorial, we will discuss how to create a WCF service using C#.Net and how to host that WCF service as a windows service in C#.Net.

Also, we will check how to fix error, Error 1001:The specified service has been marked for deletion, IIS specified authentication schemes ‘Basic, Anonymous’ error in WCF and Type System.Threading.Tasks.Task1[System.String] cannot be serialized error in WCF.

Create WCF service using C#.Net

Now, we will see how to create a WCF service using C#.Net.

Open Visual Studio 2010 and Click on File -> New Project -> Go to the WCF project template and then select WCF Service Library.

wcf service example

The project contains one App.config file, Iservice1.cs file, and Service1.cs file. You can write your code in the Service1.cs file. I added a simple method to this class as below.

In the IService1.cs add

[OperationContract]
 string GetName(string firstName, string lastName);

Refer figure below:

wcf service example c#

And implement the method in the Service1.cs as below.

public string GetName(string firstName, string lastName)
 {
 return firstName + " " + lastName;
 }
wcf service example c# step by step

Then change the App.config file which is very much important. Change the things as below:

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
 <system.web>
 <compilation debug="true" />
 </system.web>
 <!-- When deploying the service library project, the content of the config file must be added to the host's
 app.config file. System.Configuration does not support config files for libraries. -->
 <system.serviceModel>
 <services>
 <service name="WcfTestLib.Service1"
 behaviorConfiguration="WcfTestLib.Service1Behavior">
 <host>
 <baseAddresses>
 <add baseAddress = "net.tcp://localhost:8732/Design_Time_Addresses/WcfTestLib/Service1"/>
 </baseAddresses>
 </host>
 <!-- Service Endpoints -->
 <!-- Unless fully qualified, address is relative to base address supplied above -->
 <endpoint name="NetTcpEndPoint" address ="" binding="netTcpBinding"
 contract="WcfTestLib.IService1"/>
 <!--
 Upon deployment, the following identity element should be removed or replaced to reflect the
 identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
 automatically.
 -->
 <!--<identity>
 <dns value="localhost"/>
 </identity>
 </endpoint>-->
 <!-- Metadata Endpoints -->
 <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
 <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
 <endpoint name="NetTcpMetadataPoint" address="mex"
 binding="mexTcpBinding" contract="IMetadataExchange"/>
 </service>
 </services>
 <behaviors>
 <serviceBehaviors>
 <behavior name="WcfTestLib.Service1Behavior">
 <!-- To avoid disclosing metadata information,
 set the value below to false and remove the metadata endpoint above before deployment -->
 <serviceMetadata httpGetEnabled="false"/>
 <!-- To receive exception details in faults for debugging purposes,
 set the value below to true.  Set to false before deployment
 to avoid disclosing exception information -->
 <serviceDebug includeExceptionDetailInFaults="False" />
 </behavior>
 </serviceBehaviors>
 </behaviors>
 </system.serviceModel>
 </configuration>

Remember this step is very important. so be careful while changing the baseAddress, endpoint, and serviceBehaviors.

Now our WCF service work finished.

Host WCF service as windows service in C#.Net

Now we need to create a window service that will host the WCF service. For this go to the Solution right-click on it. Add new project and select Windows service from there. Then your windows service project will appear in the solution explorer. Here first we need to give two references.

The first one is the WCF project that we have made first. For this click on References -> Add Reference and select the project section by visiting the project section from the reference window. And then Give reference to the System.ServiceModel namespace from a the.Net section in the Add Reference window.

After the reference work complete, Copy the App.config file from the WCF project and Paste it into the Window service project. Here no need to change anything.

In the next step right click on the Service1.cs file and click on view code section. We have to override the OnStart () and OnStop() methods. First give reference to using System.ServiceModel;

Then write ServiceHost myHost = null; in the class and override the methods as follows.

protected override void OnStart(string[] args)
 {
 myHost = new ServiceHost(typeof(WcfTestLib.Service1));
 myHost.Open();
 }

Here one thing to remember is that the typeof contains WcfTestLib which is my WCF library project.

protected override void OnStop()
 {
 myHost.Close();
 }

The full code will look as below.

wcf service application example

In the next step open the Service1.cs designer page Right-click and Add Installer. From there you select serviceInstaller1 and select F4.

And in the properties, You can give the display name that will appear in the list of services in the control panel. Also, you can select the start type. See the figure below.

wcf service application visual studio 2017

Then click on the serviceProcessInstaller1 and click on F4. And in the properties window change the Account to NetworkService as shown in the figure below.

wcf service application

Now you can rebuild the project and our windows service is ready to install. You can install it by using the command InstallUtil.exe but I am here going to describe how you can install it by creating a setup project.

To create a setup project go to the solution explorer, right-click on it then Add a new project then select other project types -> then Setup and Deployment and then Visual Studio Installer and the Select Setup project template as shown in the figure below.

host wcf service as windows service

Then right click on the setup of the project and Add -> Project output as shown in the figure below.

Create and host wcf service as windows service

The from the Add project output group window select your Window service project and select Primary output and click on ok as shown in the figure below.

wcf service tutorial

Then right click on it and select View-> Custom actions as shown below.

Create and host wcf service as windows service in C#.Net

Then right-click on Custom Actions and click on Add custom action and click on Application folder in the Select Item in Project window and select the primary output. The final window will appear like below figure.

host wcf service as windows service c#

Now your code is ready to install and then you can go to the debug folder of the setup project and click on setup.exe or setup.msi it will install the service. And to check to write services.msc in the Run window and click on ok. And from the list of services, you can check the service.

To check whether it’s working or not you can write a separate windows application and then in the reference part click on Add service reference and in the Address window give the base address URL like
net.tcp://localhost:8732/Design_Time_Addresses/WcfTestLib/Service1/mex or you can copy the URL from the App.config file. If the service is working then you can get a result like 1 service found.

Error 1001:The specified service has been marked for deletion

I got this error while trying to install a setup (wcf and windows service) in C#.Net. The error message is “Error 1001 : The specified service has been marked for deletion”.

Error 1001:The specified service has been marked for deletion

You can try out some solution:

  • If service manager is open the close service manager (services.msc).
  • Check in the task manager if the service is running.
  • The best solution is restart the machine.

This will help to fix error Error 1001:The specified service has been marked for deletion.

IIS specified authentication schemes ‘Basic, Anonymous’ error in WCF

Now, let us see how to fix error IIS specified authentication schemes ‘Basic, Anonymous’ error in WCF. The error message came while deploying the WCF service.

Full Error Message:
IIS specified authentication schemes ‘Basic, Anonymous’, but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used.

To fix this issue follow the below solution:

Add the below line in the config file:

<basicAuthentication enabled="false" />

The config entry will look like below:

<system.webServer>
    <security>
        <authentication>
            <basicAuthentication enabled="false" />
        </authentication>
    </security>
</system.webServer>

This will help to fix error IIS specified authentication schemes ‘Basic, Anonymous’ which comes in WCF.

Type System.Threading.Tasks.Task1[System.String] cannot be serialized error in WCF

I faced this issue when trying to consume a WCF service in C#.Net. The error comes as:

Type ‘System.Threading.Tasks.Task`1[System.String]’ cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.

If you are using svcutil to generate the proxy class from the WCF url, then we need to add one more attribute to it and the error will go.

Here we are using like below:

svcutil /language:cs /out:generatedproxy.cs /config:app.config net.tcp://localhost/Design_Time_Addresses/SLicense/Service1/

Now we need to add the /syncOnly attribute at the end like below:

svcutil /language:cs /out:generatedproxy.cs /config:app.config net.tcp://localhost/Design_Time_Addresses/SLicense/Service1 /syncOnly

The will generate the generatedproxy.cs file. Use this .cs file, the error will not come.

System.InvalidOperationException:Cannot have two operations in the same contract with the same name

This error comes when I do method overloading in WCF in C#.Net. When I implemented two methods having the same name in WCF I get an error “System.InvalidOperationException:Cannot have two operations in the same contract with the same name”.

So to solve this error we have to implement these two methods using the NAME parameter like below.

[ServiceContract] public interface IService1
{
[OperationContract(Name="ShowIntegerData")] string DisplayData(int value);
[OperationContract(Name="ShowStringData")] string DisplayData(string value);
}

Service cannot be started. System.InvalidOperationException: Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding

I got the below error when trying to start a wcf service. The full error message is:
Service cannot be started. System.InvalidOperationException: Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].
at System.ServiceModel.ServiceHostBase.MakeAbsoluteUri(Uri relativeOrAbsoluteUri, Binding binding, UriSchemeKeyedCollection baseAddresses)
at System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host,

The above error was coming because of miss match in endpoint address and bases address. In the endpoint i put binding=”netTcpBinding” but in the base address I was giving the http://URL.

To fix the issue, I modified my baseAddress url and it works for me.

<baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/Design_Time_Addresses/WCFTest/Service1/"/>
</baseAddresses>

And my endpoint is:

<endpoint name="NetTcpEndPoint" address="" binding="netTcpBinding" contract="WCFTest.IService1″/>

I hope this will help to fix the error.

The requested content appears to be script and will not be served by the static file handler

I got this error when I tried to use a WCF. The full error message is “The requested content appears to be script and will not be served by the static file handler.”

There might have been the different solution I will explain here which solution works for me.

Go to Inetmgr and Right-click on the particular site and go to Manage Application and then click on Advanced Settings… as shown in the figure below:

The requested content appears to be script and will not be served by the static file handler

Then in the Advanced Settings dialogbox. Click on Application Pool and I have changed to ASP.NET v4.0 as shown in the fig as below and now it works fine.

The requested content appears to be script and will not be served by the static file handler wcf

This will fix the error “The requested content appears to be script and will not be served by the static file handler”

You may like following C#.Net tutorials:

This tutorial we learned how to create a wcf service using C#.Net and how to host a wcf service as windows service in C#.Net. And also, we learned how to fix error, Error 1001:The specified service has been marked for deletion in C#.Net.

Also, we checked how to fix error, IIS specified authentication schemes ‘Basic, Anonymous’ error in WCF and Type System.Threading.Tasks.Task1[System.String] cannot be serialized error in WCF.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>