当前位置: 首页 > 编程语言 > C#

如何在代码中设置ServiceHostingEnvironment.AspNetCompatibilityEnabled = true(不在配置中).NET - C#分享

时间:2023-04-10 13:19:30 C#

C#学习教程:HowtosetServiceHostingEnvironment.AspNetCompatibilityEnabled=trueincode(notinconfiguration).NET/C#h从RESTfulWCF服务访问HttpContext.Current。我知道我可以通过在配置中添加以下内容来实现此目的:并在我的服务中使用以下属性:[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]测试,所以我不能使用配置文件来指定服务行为等。目前我的代码如下所示,但尽管在网上进行了搜索,但我还是无法弄清楚如何在不使用配置的情况下设置ServiceHostingEnvironment类并将AspNetCompatibilityEnabled属性设置为true,有人可以帮忙吗?stringserviceUrl="http://localhost:8082/MyService.svc";_host=newServiceHost(typeof(MyService),newUri[]{newUri(serviceUrl)});ServiceEndpointserviceEndpoint=_host.AddServiceEndpoint(typeof(IMyService),newWebHttpBinding(),string.Empty);serviceEndpoint.Behaviors.Add(newWebHttpBehavior());//这就是我卡住的地方,我需要类似...ServiceHostingEnvironmentSectionshes=newServiceHostingEnvironmentSection();她。AspNetCompatibilityEnabled=true;_host.Add(她);_host.Open();非常感谢任何帮助,并提前致谢。考虑将HttpContext.Current的显式使用分解为一个可以在单元测试期间存根的接口。HttpContext.Current仅在您的wcf服务托管在asp.netweb应用程序中时定义-如果有一天您需要将其作为普通wcf服务托管,则HttpContext.Current将无法工作。你可以做到这一点,我不知道这些其他答案是什么,但他们逃脱了!做类似的事情:_host=newServiceHost(...);//删除现有行为,因为它是只读的(inti=0;i-编辑这将删除现有行为(如果存在),然后添加具有该模式的新行为的行为。我的示例将其设置为.Allowed,但您当然可以将其设置为您想要的任何模式。这是一个AppDomain范围的设置,您可以在System.ServiceModel中的静态ServiceHostingEnvironment类上设置:ServiceHostingEnvironment.AspNetCompatibilityEnabled=true;这应该在您创建和打开服务主机。本来不错-但它是一个只读设置,唯一的设置方法似乎是通过配置:-(详细说明AustinHarris的回答:你需要更改ServiceHost的行为。因为该属性是只读的,你需要删除并读取行为到ServiceHost。如果你有一个控制台应用程序或一个windows服务,这个servicehost将被定义。像这样:publicstaticvoidMain(){using(ServiceHostserviceHost=newServiceHost(typeof(CalculatorService))){try{//打开ServiceHost开始监听消息。serviceHost.Open();//现在可以访问服务了。Console.WriteLine("服务准备就绪。");Console.WriteLine("按下终止服务。");控制台.ReadLine();//关闭ServiceHost.serviceHost.Close();catch(TimeoutExceptiontimeProblem){Console.WriteLine(timeProblem.Message);控制台.ReadLine();}catch(CommunicationExceptioncommProblem){Console.WriteLine(commProblem.Message);控制台.ReadLine();}}}在这种情况下,接下来,AustinHarris的代码就足够了(如果他没有写Allowed而不是Required...)。但是,如果将WCF服务集成到ASP.NET应用程序中,棘手的部分是获取ServiceHost。关键是YOUR_SERVICE.svc标记文件中的工厂属性。然后你需要自己写工厂。下面的代码是VB.NET,我会把它留给读者把它翻译成C#(顺便说一下,你需要将WITH_FORMS_AUTHENTICATION设置为true,C#ps:http://converter.telerik.com)'ImportsSystem.ServiceModelImportsSystem.ServiceModel.Description'ImportsSystem.ServiceModel.Dispatcher'ImportsSystem.ServiceModel.Channels'ImportsSystem.ServiceModel.ConfigurationImportsSystem.ServiceModel.Activation'添加对程序集System.ServiceModel.Activation.dll公共类的引用DerivedHost继承ServiceHostPublicSubNew(tAsType,ParamArraybaseAddresses()AsUri)MyBase.New(t,baseAddresses)EndSubProtectedOverridesSubOnOpening()'Me.Description.Behaviors.Add(Newmys)'Me.Description.Add(NewMyServiceBehavior())'Me.Description.Behaviors.Add(NewWcfMessageLoggerExtension())MyBase.OnOpening()EndSubEndClass'DerivedHost'http://msdn.microsoft.com/en-us/library/aa702697(v=vs.110).aspx公共类DerivedFactory继承ServiceHostFactoryProtectedOverrides函数CreateServiceHost(tAsType,baseAddressesAsUri())AsServiceHostReturnNewDerivedHost(t,baseAddresses)EndFunction'CreateServiceHost'然后在CreateServiceHost方法中,我们可以做所有在自托管情况下可以做的事情:PublicOverridesFunctionCreateServiceHost(serviceAsString,baseAddressesAsUri())AsServiceHostBaseApplication.ConfigData.ReadConfigData()'这里忽略服务参数,因为我们知道我们的服务。DimserviceHostAsNewServiceHost(GetType(ImportBelegung),baseAddresses)'System.ServiceModel.ServiceHostingEnvironment.AspNetCompatibilityEnabled=True'http://stackoverflow.com/questions/13597408/wcf-message-inspector-is-not-working'DimserviceHost作为新的System.ServiceModel.ServiceHost(GetType(ImportBelegung))'serviceHost.Description.Behaviors.Add(NewWcfMessageLoggerExtension())'http://stackoverflow.com/questions/5907791/how-to-programatically-create-a-wcf-service-and-its-metadata-on-the-same-url'http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost(v=vs.110).aspx'host.Open()'此示例遍历所有ServiceEndpoint对象并将ConsoleMessageTracing添加为终结点行为:对于每个端点作为ServiceEndpointInserviceHost.Description.Endpoints'endpoint.Behaviors.Add(NewWcfMessageLoggerExtension())'endpoint.Behaviors.Add(NewConsoleOutputBehaviorExtensionElement)endpoint.Behaviors.Add(NewMessageInspector.ConsoleOutputBehavior)endpoint.Behaviors.Add(NewHeaderInspector.ConsoleOutputHeaderBehavior)Nextendpoint'Ensure(in)aspNetCompatibilityEnabled="true"-->#ConstWITH_FORMS_AUTHENTICATION=False#IfWITH_FORMS_AUTHENTICATIONThenForiAsInteger=0ToserviceHost.Description.Behaviors.Count-1步骤1如果TypeOfserviceHost.Description.Behaviors(i)IsAspNetCompatibilityRequirementsAttributeThenserviceHost.Description.Behaviors.RemoveAt(i)ExitForEndIfNextiserviceHost.Description.Behaviors.Add(NewAspNetCompatibilityRequirementsAttribute()With{.RequirementsMode=AspNetCompatibilityRequirementsMode.Required})#EndIfReturnserviceHostEndFunction'CreateServiceHostEndClass'DerivedFactory在深入研究Reflector之后,我能够使用反射设置AspNetCompatibilityEnabled标志这种方法有明显的缺点,但它为我完成了工作:以上是C#学习教程:如何在代码中设置ServiceHostingEnvironment.AspNetCompatibilityEnabled=true(而不是在配置中)是.NET/C#共享的所有内容(如果有用的话)给大家,需要了解更多有关C#学习教程的信息。希望大家多多关注---//通过调用内部静态方法获取ServiceHostingEnvironmentSectionvarsection=(ServiceHostingEnvironmentSection)typeof(ServiceHostingEnvironmentSection).GetMethod("UnsafeGetSection",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Static).Invoke(null,null);//将只读标志设置为false,以便可以更新值(部分,错误);//设置AspNetCompatibilityEnabled值section.AspNetCompatibilityEnabled=true;//现在可以添加服务路由routes.Add(newServiceRoute("MyRoutePrefix",newServiceHostFactory(),typeof(MyService)));本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: