How to host my WCF service in my website?

I am new to WCF. I have developed a sample WCF service. My service uses the basicHttp binding.

I host my service in local IIS 7.5 using WAS and it works fine. Now I want to host my service in my website.

I search Google but there most of them are hosted in localhost in IIS. Please tell me how to I do that? It will be better to refer some tutorial or step by step guide.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have basically two options, I believe:

Option 1 – “bin” deploy (preferred option)

  1. compile your WCF service into a DLL (class library)
  2. create a website in IIS
  3. copy the WCF DLL’s into the website’s .bin folder
  4. create a *.svc file in that website
  5. add an appropriate web.config in the website folder to define your endpoints and service configuration etc.

Your WCF service will now be reachable at the website’s base address, plus the name of the *.svc file, e.g.

http://myserver/someweb/Myservice.svc

Your *.svc would look something like this:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="WCF_Simple_Service.HelloIndigoService"  %>

The Service= attributes denotes the class implementing the service – fully qualified with its namespace.

Option 2 – put stuff into App_Code

  1. create a website in IIS
  2. put all your WCF related *.cs files directly into the .App_Code folder
  3. create a *.svc file in that website
  4. add an appropriate web.config in the website folder to define your endpoints and service configuration etc.

Your WCF service will now be reachable at the website’s base address, plus the name of the *.svc file, e.g.

http://myserver/someweb/Myservice.svc

Your *.svc would look something like this:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="Service" 
    CodeBehind="~/App_Code/Service.cs" %>

A simple, sample web.config might look something like this:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WithDebug">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  <services>
    <service name="SimpleWCF.HelloIndigoService" behaviorConfiguration="WithDebug">
      <endpoint
          address=""
          binding="basicHttpBinding"
          contract="SimpleWCF.IHelloIndigoService" />
      <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />
    </service>
  </services>
</system.serviceModel>

You basically define your <service> tag – and again: the name= denotes the class implementing the service – fully qualified with its namespace. It must contain at least one endpoint – a “mex” endpoint is optional – but very useful, especially for development and testing. It allows client to “discover” the service and get its service description so it can interface with it.

Once your service is deployed in IIS, you can see it in action using a tool like the WCF Test Client that ships for free with WCF, or SoapUI which is a general-purpose SOAP testing utility (with a free edition for you to use).

Method 2

old thread, if someone finds it… but instead of your *.svc you can also create in your global.asax:

static Global()
{
    RouteTable.Routes.Add(new ServiceRoute("ExternalServices/SOAP/test", new     ninjectServiceHostFactory(), typeof(testService)));
}

Method 3

In addition to Marc’s answer, if you add factory attribute to your *.svc file like the one below, you dont need to change ANYTHING in web.config file which is in your main project. Let me repeat it again. no massive mess in web.config file, leave it as is. It’s due to the fact the Factory attribute in the svc file which take care if it all.

<%@ ServiceHost Language="C#" Debug="true" Service="WCF_Simple_Service.HelloIndigoService" Factory="System.ServiceModel.Activation.WebServiceHostFactory"  %>

But I must say that when you call your service, it will give endpoint not found error. But when you call your methods in your service, they will all work. tested in restful wcf.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x