Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure WCF service from code when hosted in IIS?

My WCF service exposes an https AND an http endpoint. Apart from the SSL they are identical. They map to the same code.

The ultimate intention is for external users to connect via https, internal users to use http.

In development this gives me a problem. Cassini, the development web server packaged in VS, hates SSL.

I'm wondering if I can configure the service from code, so when running under Cassini, I would not configure https.

Hence the question - How do I configure the service from code if it is IIS hosted? I'd be very happy with alternative answers on how I can persuade Cassini to NOT complain about the https part of the configuration.

like image 828
RichardHowells Avatar asked Nov 24 '10 17:11

RichardHowells


People also ask

What are 3 basic WCF configuration required for hosting a WCF service?

There are three types of hosting environments for WCF services: IIS, WAS, and self-hosting. The term “self-hosting” refers to any application that provides its own code to initialize the hosting environment. This includes console, Windows Forms, WPF, and managed Windows services.

How do I run a WCF service locally?

To open WCF Test Client, open Developer Command Prompt for Visual Studio and execute WcfTestClient.exe. Select Add Service from the File menu. Type http://localhost:8080/hello into the address box and click OK. Make sure the service is running or else this step fails.

How restart WCF in IIS?

Simply start and start the web app within the IIS interface to start and stop the service. I know it's an old post but, your answer is correct only to http endpoints. E.g. endpoints based on net. tcp protocol will be still available.


2 Answers

"IIS will take care of spinning up the necessary ServiceHost based on your *.svc file - not a whole lot you can do about that, really."

Not too close to the truth. Exactly in the SVC file of your service there is attribute named Factory. Where you can specify the the class and the assebly where the class is located. This class may be your own descendant of Web|DataServiceHostFactory So your svc markup would look like this

<%@ ServiceHost 
Language="C#"
 Debug="true" 
 Service="name.space.myService" 
 CodeBehind="name.space.myService.svc.sc" 
 Factory = "name.space.WebServiceHostFactoryEx, assembly.name"
 %>

The WebServiceHostFactory will be created for every service hit and will recreate your host the way you want it.

You will also need to inherith WebServiceHost and create it the way you need it with certain endpoins, behaviors, addresses, etc settings - whatever you like.

There is very nice post from Michele Bustamante here

EDIT: I figured out the above link is not working anymore, so here it is another one.

I am using this in IIS hosted enviroment for couple of services that are initialized same way.

like image 52
ChristoD Avatar answered Nov 15 '22 02:11

ChristoD


When you're hosting in IIS, you're leaving a lot of care taking into the realm of IIS - you cannot really grab a hold of your service in this case.

IIS will take care of spinning up the necessary ServiceHost based on your *.svc file - not a whole lot you can do about that, really.

My solution would be different - externalize the <service> tag in your configuration file (web.config):

<system.serviceModel>
  <services>      
     <service configSource="service.dev.config" />
  </services>
</system.serviceModel>

In your dev environment, only expose the http endpoint - so your service.dev.config would look something like this:

<service name=".....">
    <endpoint name="default"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="insecure"
              contract="......" />
</service>

Create a second service.prod.config which then contains both endpoints - http and https:

<service name=".....">
    <endpoint name="default"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="insecure"
              contract="......" />
    <endpoint name="secure"
              address="....."
              binding="basicHttpBinding" bindingConfiguration="secure"
              contract="......" />
</service>

and reference that in your web.config on the deployment server.

like image 20
marc_s Avatar answered Nov 15 '22 00:11

marc_s