Run WCF ServiceHost with multiple contracts

前端 未结 8 2220
长发绾君心
长发绾君心 2020-11-27 14:09

Running a ServiceHost with a single contract is working fine like this:

servicehost = new ServiceHost(typeof(MyService1));
servicehost.AddServiceEndpoint(typ         


        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 14:40

    This answer is a further response to the comment in the accepted answer from chilltemp.

    Sam, You really should determine why you need 10-50 contracts and try to find another solution. I looked over Juval Lowy's WCF Coding Standards (found on http://www.idesign.net/) and found the following references:

    3 Service Contracts ... 4. Avoid contracts with one member. 5. Strive to have three to five members per service contract. 6. Do not have more than twenty members per service contract. Twelve is probably the practical limit.

    He doesn't mention a limit on contract implementations (that I can find) but I can't imagine him viewing 50 contracts on a service as anything resembling a best practice. One solution I have found that works well is to use member sharing for similar functions.

    For instance, if you are using the WCF service to perform mathematics on 2 values you might have 4 members on the service side: Add(x,y), Subtract(x,y), Multiply(x,y), Divide(x,y). If you combine these into a more generic member and use an object to pass the needed data you can easily reduce your member count and increase scalability. Example: PeformCalculation(obj) where obj has x, y, and action (add, subtract, multiply, divide) properties.

    Hope this helps.

提交回复
热议问题