C# 通过代码而不是配置创建WCF服务(不是客户端)

C# 通过代码而不是配置创建WCF服务(不是客户端),c#,.net,web-services,wcf,C#,.net,Web Services,Wcf,我有大量托管在IIS上的WCF服务 所有这些服务都具有相同的system.serviceModel配置。它们都有相同的绑定配置、行为配置,唯一不同的是服务的契约,它被放在不同的自定义配置文件中,用于不同的用途 现在,我在system.serviceModel部分所做的每一项更改都需要在所有服务中完成,这很烦人 由于我使用自定义C#代码创建WCF客户端并对其进行更新以适应服务,因此我考虑以某种方式通过C#代码创建服务的system.serviceModel(每次更改都将是dll更新或其他内容) 所

我有大量托管在IIS上的WCF服务

所有这些服务都具有相同的
system.serviceModel
配置。它们都有相同的绑定配置、行为配置,唯一不同的是服务的契约,它被放在不同的自定义配置文件中,用于不同的用途

现在,我在
system.serviceModel
部分所做的每一项更改都需要在所有服务中完成,这很烦人

由于我使用自定义C#代码创建WCF客户端并对其进行更新以适应服务,因此我考虑以某种方式通过C#代码创建服务的
system.serviceModel
(每次更改都将是dll更新或其他内容)

所以,我想通过代码创建服务是可能的。 我还猜测,可以通过创建自定义的
ServiceHostFactory
来完成,但我确实找不到可以选择服务绑定的位置


实现这一目标的最佳方法是什么?

这里有一个代码示例似乎符合您的问题:我将它复制到这里以供参考

装订

// Specify a base address for the service
String baseAddress = "http://localhost/CalculatorService";
// Create the binding to be used by the service.
BasicHttpBinding binding1 = new BasicHttpBinding();
终点呢

using(ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
    host.AddServiceEndpoint(typeof(ICalculator),binding1, baseAddress);
现在,这是另一个服务工厂

public class ServiceHostFactory : 

    System.ServiceModel.Activation.ServiceHostFactory
        {

            protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            {
                var host = base.CreateServiceHost(serviceType, baseAddresses);
                host.Description.Behaviors.Add(ServiceConfig.ServiceMetadataBehavior);
                ServiceConfig.Configure((ServiceDebugBehavior)host.Description.Behaviors[typeof(ServiceDebugBehavior)]);
                return host;
            }
        }
在代码中完成配置后:

public static class ServiceConfig
{
    public static Binding DefaultBinding
    {
        get
        {
                var binding = new BasicHttpBinding();
                Configure(binding);
                return binding;
            } 
        } 

        public static void Configure(HttpBindingBase binding)
        {
            if (binding == null)
            {
                throw new ArgumentException("Argument 'binding' cannot be null. Cannot configure binding.");
            }

            binding.SendTimeout = new TimeSpan(0, 0, 30, 0); // 30 minute timeout
            binding.MaxBufferSize = Int32.MaxValue;
            binding.MaxBufferPoolSize = 2147483647;
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
            binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
            binding.ReaderQuotas.MaxDepth = Int32.MaxValue;
            binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;
            binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
        }

        public static ServiceMetadataBehavior ServiceMetadataBehavior
        {
            get
            {
                return new ServiceMetadataBehavior
                {
                    HttpGetEnabled = true, 
                    MetadataExporter = {PolicyVersion = PolicyVersion.Policy15}
                };
            }
        }

        public static ServiceDebugBehavior ServiceDebugBehavior
        {
            get
            {
                var smb = new ServiceDebugBehavior();
                Configure(smb);
                return smb;
            }
        }


        public static void Configure(ServiceDebugBehavior behavior)
        {
            if (behavior == null)
            {
                throw new ArgumentException("Argument 'behavior' cannot be null. Cannot configure debug behavior.");
            }

            behavior.IncludeExceptionDetailInFaults = true;
        }
}

我错过了接受绑定的ServiceHost的ctor重载。我觉得自己很愚蠢。非常感谢。