开发者

How Do I config one endpoint for multiple service in WCF?

Is it possible and how to config to ju开发者_C百科st have one endpoint for multiple services in our WCF Service Application?

Thank you


If you mean something like:

Service1
Service2
Service3
.
.
.
Service n

And all services are at one endpoint, say http://localhost/MyServices/Services, then I believe the answer is no. The Address, Binding and Contract (ABC) make up the endpoint, and each endpoint must have a unique address.

So even if you tried something like:

Endpoint 1:

Address: http://localhost/MyServices/Services
Binding: wsHttpBinding
Contract: Service1

Endpoint 2:

Address: http://localhost/MyServices/Services
Binding: wsHttpBinding
Contract: Service2

You'd run into problems with endpoint 2 as it has the same address as endpoint 1.

Specifying an Endpoint Address

Hosting Multiple Services


Every Service / Binding / Contract combination must use a discrete address and therefore must be a separate endpoint

However, as far as your clients are concerned, as long as you use the same transport protocol you could use the WCF 4 routing service to give a single addressable endpoint and then use other criteria (say action or other SOAP header) to route to the correct service


One work around would be to implement partial classes that allow you to separate your content in individual cs files while maintaing a single interface and endpoint. This isn't the most ideal way, because at the end of the day it is still a single class made up of partial classes, but at least it looks like you have separate services because you have a class file for each service.

Example Structure:

IMyService.cs

[ServiceContract]
public interface IMyService
{
   [OperationContract]
   string GenericMethod()

   [OperationContract]
   string GetUsers(int companyId)

   [OperationContract]
   string GetMessages(int userId)

}

MyService.cs

//Put any attributes for your service in this class file
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public partial class MyService : IMyService
{
  public string GenericMethod() 
  {
     return "";
  }
}

UserService.cs

public partial class MyService
{
    public string GetUsers(int companyId) 
    {
       return "";
    }
}

MessagingService.cs

public partial class MyService
{
      public string GetMessages(int userId) 
      {
          return "";
      }
}


What you can do is to create a new interface that exposes all the interfaces you want to expose and then have one class that just delegates the request to the right class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜