未托管在 IIS 中时,通过 HTTP 公开 WCF 服务 [英] Exposing WCF Services Via HTTP when not hosted in IIS
问题描述
正如标题所说,我们需要在 .NET 应用程序和 Adobe AIR 应用程序之间设置 WCF 服务.我们不想在机器上运行 IIS,而是更愿意安装和运行托管在 Windows 服务中的 WCF 服务.
但是,我不确定这样做会让我们使用 HTTP 作为传输,或者它是否只能在 IIS 中工作?我能够设置一些东西以使用 TCP 传输,但这与使用 AIR 的互操作性不如使用 HTTP.
我一直在使用一些测试代码来查看它是否有效:
常规控制台应用:
static void Main(){使用 (ServiceHost host = new ServiceHost(typeof(TestService))){主机.Open();}Console.WriteLine("等待中...");Console.ReadLine();}
TestService 是一个简单的 HelloWorld 类型服务.
在 App.Config 中:
<预><代码><配置><system.serviceModel><服务><服务名称="WCFExample2.TestService" behaviorConfiguration="WCFExample2.TestServiceBehavior"><主机><基地址><add baseAddress = "http://localhost:8731/Design_Time_Addresses/WCFExample2/Service1/"/></baseAddresses></host><!-- 服务端点--><!-- 除非完全限定,否则地址是相对于上面提供的基地址--><端点地址="" binding="wsHttpBinding" contract="WCFExample2.ITestService"><!--部署时,应删除或替换以下标识元素以反映部署的服务在其下运行的身份.如果删除,WCF 将推断出适当的身份自动地.--><身份><dns value="localhost"/></身份></端点><!-- 元数据端点--><!-- 服务使用元数据交换端点来向客户端描述自身.--><!-- 此端点不使用安全绑定,应在部署前进行保护或删除--><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/></服务></服务><行为><服务行为><行为名称="WCFExample2.TestServiceBehavior"><!-- 为避免泄露元数据信息,将下面的值设置为 false 并在部署之前删除上面的元数据端点 --><serviceMetadata httpGetEnabled="True"/><!-- 接收故障中的异常详细信息以进行调试,将下面的值设置为 true.部署前设置为false避免泄露异常信息--><serviceDebug includeExceptionDetailInFaults="False"/></行为></serviceBehaviors></行为></system.serviceModel></配置>除了配置文件设置之外,还有一件事需要考虑.如果您在 Windows 服务中自托管,则为 http 端点
- 使服务登录帐户成为机器上的本地管理员或
- 您必须使用 http.sys 为 http 命名空间注册服务帐户.此步骤必须由本地管理员完成,但在每台机器中仅执行一次.您可以使用 HttpSysCfg 工具在 XP/win 2003 中执行此操作. 对于 vista/win 2008,请使用 netsh.
Like the title says, we need to set up WCF services between a .NET app, and a Adobe AIR app. We don't want to run IIS on the machine, and would much prefer to install and run the WCF services hosted within a windows service.
However, I am uncertain of doing that will let us use HTTP as the transport, of does that only work within IIS? I was able to set things up to use the TCP transport, but that doesn't interop with AIR nearly as nice as using HTTP.
EDIT: Some test code I've been using to see if this works:
Regular console app:
static void Main()
{
using (ServiceHost host = new ServiceHost(typeof(TestService)))
{
host.Open();
}
Console.WriteLine("Waiting...");
Console.ReadLine();
}
TestService is a simple HelloWorld type service.
In the App.Config:
<configuration>
<system.serviceModel>
<services>
<service name="WCFExample2.TestService" behaviorConfiguration="WCFExample2.TestServiceBehavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8731/Design_Time_Addresses/WCFExample2/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="wsHttpBinding" contract="WCFExample2.ITestService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFExample2.TestServiceBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Apart from the config file settings one more thing to consider. If you selfhost in a windows service, a http endpoint then
- Make the service login account a local admin on the machine or
- You have to register the service account for the http namespace with http.sys. This step has to be done by a local admin but only once in each machine. You can use the HttpSysCfg tool to do this in XP/win 2003. For vista/win 2008 use netsh.
这篇关于未托管在 IIS 中时,通过 HTTP 公开 WCF 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!