开发者

Adding a parameter to a WCF .svc file

I would like to be able to pass application specific configuration information to my WCF by placing a parameter in the service.svc file.

I have a standard error logging web service that I call from different apps. I would like to be able to call the service with different names and have the service know how it was called a开发者_如何学运维nd write to different files. For instance, the app will call Warning.svc, Serious.svc, or Critical.svc and depending on which is called write the message to the appropriate file.

So my thought is to put a parameter in the .svc file telling the WCF how it was called. If not that, is there a means to get the name of the svc file from within my code?

I know that I could have different Methods for each call, but that would require modifying the WCF when I want to add an additional type. Whereas is I could just add another .svc the functioality would be added without having to modify any code.


You can specify a custom ServiceHostFactory in the svc file <%@ ServiceHost %> tag then use the service attribute for whatever you want. This value will be passed to your SHFactory where you can parse it and init your ServiceHost with it.

Here's an example that just mirrors the default WCF behavior:

Service1.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService2.Service1" Factory="WcfService2.CustomServiceHostFactory" CodeBehind="Service1.svc.cs" %>

CustomServiceHostFactory.cs:

namespace WcfService2
{
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Activation;

    public class CustomServiceHostFactory : ServiceHostFactoryBase
    {
        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
        {
            var host = new ServiceHost(Type.GetType(constructorString), baseAddresses);
            host.AddDefaultEndpoints();
            return host;
        }
    }
}


I don't think you can do this in the .svc file.

However, this seems quite a convoluted way of going about what you want to do, and is not typical web service style at all. You shouldn't have too many endpoints for essentially the same web service with only minor differences. This will be a nightmare to maintain in the future.

Why can't you modify your methods to accept one more parameter, which is a string or enum or whatever that indicates which file you want to log to? If passed null, then write to the default one. This is how it is typically done in common logging packages.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜