如何在代码而不是配置中创建WCFEndPointBehaviors?我有以下Xml配置,我想在C#代码中实现,而不是使用配置。我不知道是谁使用webHttp将EndPoint公开为REST服务。ServiceHostserviceHost=newServiceHost(singletonInstance,"http://localhost:1234/MyService/xml");//创建元行为ServiceMetadataBehaviorbehavior=newServiceMetadataBehavior();行为.HttpGetEnabled=true;serviceHost.Description.Behaviors.Add(行为);绑定mexBinding=MetadataExchangeBindings.CreateMexHttpBinding();serviceHost.AddServiceEndpoint(typeof(IMetadataExchange),mexBinding,"mex");WSHttpBindinghttpBinding=newWSHttpBinding(SecurityMode.None);serviceHost.AddServiceEndpoint(typeof(MyService.IMy)httpBinding,"rest");通常,当使用WCF执行REST时,您要么使用配置中的行为,要么使用WebServiceHost类(而不是“普通的”ServiceHost)。使用WebServiceHost包括所有必要的调整和点点滴滴以使REST工作-不再有webHttp行为。当然,这意味着您需要一个单独的WebServiceHost(在System.ServiceModel.Web中)专门作为REST托管服务。这可能是也可能不是您想要的:WebServiceHostwebServiceHost=newWebServiceHost(singletonInstance,"http://localhost:1234/MyService/xml");WebHttpBindingwebBinding=newWebHttpBinding();webServiceHost.AddServiceEndpoint(typeof(MyService.IMyService),webBinding,"rest");您拥有的另一个选择是将服务端点添加到常规服务主机并仅在该端点上配置webhttp行为-端点(和服务)行为只是一个常规的.NET类,毕竟您可以实例化并添加到相应的行为集合(在服务上或在单个端点上):WebHttpBindingrestBinding=newWebHttpBinding();ServiceEndpointrestSEP=serviceHost.AddServiceEndpoint(typeof(MyService.IMyService),restBinding,"rest");restSEP.Behaviors.Add(newWebHttpBehavior());我希望这两种方式都能让你得到你想要的!(或者至少让你更接近:-)C#学习教程就是这些:如何在代码而不是配置中创建WCFEndPointBehaviors?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
