Anton(Bug)

There is a question about hosting one or more WCF service libraries under IIS/WAS. Assume we have a huge pack of services in several libraries, and I want to host them under IIS/WAS without creating a bunch of *.svc files and config settings. Settings for all services are identical. How can I correctly host them under IIS/WAS

Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Anton(Bug)

Does anyone know the answer

Or WCF wasn't designed for Enterprise Applications that have many services in it





Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Dwight Goins

Use a serviceHostFactory. Within the factory, figure out which service needs to be created and return that service.

Here's a link:

http://msdn2.microsoft.com/en-us/library/aa702697.aspx

Here's an example:

Code Snippet

//1. Create a ServiceHost Factory class and a ServiceHost class:

namespace QuickLearn.Samples
{

public class MyServiceHostFactory : ServiceHostFactoryBase
{

public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{


//Logic to determine which ServiceHost to create

return new MyServiceHost(baseAddresses);
}
}

class MyServiceHost : ServiceHost
{

public MyServiceHost(params Uri[] addresses)
: base(typeof(MyService), addresses)
{
}

override protected void InitializeRuntime()
{

// Create the custom binding and add an endpoint to the service.
WSHttpBinding b = new WsHttpBinding();


ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
this.Description.Behaviors.Add(smb);

this.AddServiceEndpoint(typeof(IMyService), b, string.Empty);
base.InitializeRuntime();
}
}

//2. Next Create your servicecontract and service

[ServiceContract]
public interface IMyService : IDisposable
{
[OperationContract]
string Op1();
}

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyService : IMyService
{
public string Op1()
{
return String.Format("Hello");
}

public void Dispose()
{
}

}

} //end namespace

//3. Create a Service.svc file with the contents:

<%@ServiceHost language=c# Debug="true" Factory="QuickLearn.Samples.MyServiceHostFactory" %>






Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Anton(Bug)

Thanks for the answer.

Well assume that my application has ~150 services in it. Presentation tier is communicating with Bus tier through them. Presentation and Bus tiers are located on the different machines. We are thinking of using WCF for such communication. In this case all services will have identical configuration

As for configuration it is clear that we can configure bindings during run-time using ServiceFactory or other ways, but what about svc files How can we correctly host our service under IIS/WAS without them Or you suggest developer create ~150 svc files and put them somewhere





Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Dwight Goins

You could create 1 svc file, as shown above. This file registers a ServiceHostFactory, which can be used to create different types of service hosts, but only one being returned [As per Jon Flanders correction-thanks -Jon...]. Each Service Host can open the channel to communicate, in otherwords, implement a ServiceContract on a particular endpoint. (Your actual Service). I'm not sure about hosting a WCF Service without svc file in WAS.

From an ASP.NET perspective you could create a custom HttpHandler, but then you're confined to using the HttpBindings for the initial message, to start the process.

Within the ServiceHostFactory, you can parse the request coming in, to determine which ServiceHost handles that particular request. The Bindings of a service can be configured at runtime using the code above. However, there are constructors that allow the bindings to be configured using web.configuration files as well.

If you have ~150 services, you'd just need ~150 endpoints (OUCH!!!), relative to the base addresses. If all the configurations are identical, you could use the configuration option to just configure the bindings based off the same Configuration name, thus removing the need to have ~!50 endpoints.

A better approach might even be to create one service that implements a custom DispatchOperationSelector, and possibly a DispalyOperationInvoker which can handle how to determine which Service operations to invoke and so forth.

There are many designs you could use. There is one caveat to the CustomDispatchOperations... however, in these designs you would most likely program against the Messaging layer using Message classes, in order to keep the maintenance of ~150 * n contracts and messagetypes down. Which means more code to the Pres layer, the bus layer and of course the Service layer.

HTH

(Thinking out loud...)




Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Jon Flanders

Each ServiceHostFactory can only return one ServiceHost that will be opened.

One potential design for this problem (~150 contracts) is to create a servicehost that uses a "universal contract" (one that uses Message as the input/output- and Action="*" and ReplyAction="*") and adding a wsdl exporter to allow clients to generate stronly typed contracts.

Inside of the service type you'd write code to be very generic and would therefore be able to dynamically respond to new contracts (rather than having to stop and start your servicehost).






Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Dinesh Ravva - MSFT

You could dynamically create svc and config files from a template since all the settings are similar and host them in vdir's.



Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Anton(Bug)

Can you explain how can I do this If some samples exists it would be great.

Thanks for the answer.





Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Anton(Bug)

Can anyone explain how I can "dynamically create svc"





Re: Windows Communication Foundation (Indigo) Hosting ServiceLibrary(s) under IIS/WAS, how to?

Dinesh Ravva - MSFT

You could use Microsoft.Web.Administration API to create virtual directories dynamically and then host the the simple .svc files created on fly.

You can read for more details on creating vdir dynamically at http://blogs.iis.net/carlosag/archive/2006/04/17/Microsoft.Web.Administration-in-IIS-7.aspx.

I came across a sample at http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1200764&SiteID=1.