是否可以通过服务类型的实例实例化WebServiceHost,而无需无参数构造函数?

问题描述 投票:2回答:1

我正在尝试创建自托管的System.ServiceModel.Web.WebServiceHost(.NET 4),但是我遇到了构造函数的问题。有三种选择:

  1. WebServiceHost()是一个没有参数的无参数构造函数,因为没有办法指定服务的类型,甚至是契约。反思它,它没有做任何事情 - 只是一个不调用base的空默认构造函数。
  2. WebServiceHost(object singletonInstance, params Uri[] baseAddresses)我不想要一个单例实例,因为这是一个InstanceContextMode.PerCall类。
  3. WebServiceHost(System.Type serviceType, params Uri[] baseAddresses)我想要实例化为服务的类型没有无参数构造函数(这是此方法的要求)。我正在使用NInject将参数推送到构造函数中。但是,我正在尝试将其作为自托管测试运行,所以我想避免使用DI。

这些方面有什么选择,还是我不能自我托管?

wcf webservicehost
1个回答
3
投票

要支持没有无参数构造函数的服务类,您需要使用知道如何创建服务类的IInstanceProvider实现。下面的代码显示了一个WebServiceHost,您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx找到更多关于实例提供程序的代码。

public class StackOverflow_9997163
{
    [ServiceContract]
    public class Service
    {
        private int increment;
        public Service(int increment)
        {
            this.increment = increment;
        }
        [WebGet]
        public int Add(int x, int y)
        {
            return x + y + increment;
        }
    }
    class MyInstanceProvider : IInstanceProvider
    {
        Func<Service> serviceCreator;
        public MyInstanceProvider(Func<Service> serviceCreator)
        {
            this.serviceCreator = serviceCreator;
        }

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return this.serviceCreator();
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return this.serviceCreator();
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }
    }
    class MyServiceBehavior : IServiceBehavior
    {
        Func<Service> serviceCreator;
        public MyServiceBehavior(Func<Service> serviceCreator)
        {
            this.serviceCreator = serviceCreator;
        }

        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider = new MyInstanceProvider(this.serviceCreator);
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        int currentIncrement = 1;
        host.Description.Behaviors.Add(new MyServiceBehavior(delegate()
        {
            return new Service(currentIncrement++);
        }));
        host.Open();
        Console.WriteLine("Host opened");

        for (int i = 0; i < 10; i++)
        {
            WebClient c = new WebClient();
            Console.WriteLine(c.DownloadString(baseAddress + "/Add?x=6&y=8"));
        }

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.