如何在我的网站中托管我的 WCF 服务? [英] How to host my WCF service in my website?
问题描述
我是 WCF 的新手.我开发了一个示例 WCF 服务.我的服务使用 basicHttp
绑定.
I am new to WCF. I have developed a sample WCF service. My service uses the basicHttp
binding.
我使用 WAS 在本地 IIS 7.5 中托管我的服务并且它运行良好.现在我想在我的网站上托管我的服务.
I host my service in local IIS 7.5 using WAS and it works fine. Now I want to host my service in my website.
我在 Google 上搜索,但其中大部分都托管在 IIS 的 localhost 中.请告诉我该怎么做?最好参考一些教程或分步指南.
I search Google but there most of them are hosted in localhost in IIS. Please tell me how to I do that? It will be better to refer some tutorial or step by step guide.
推荐答案
你基本上有两个选择,我相信:
You have basically two options, I believe:
选项 1 - bin"部署(首选选项)
- 将您的 WCF 服务编译成 DLL(类库)
- 在 IIS 中创建网站
- 将 WCF DLL 复制到网站的
.in
文件夹中 - 在该网站中创建一个
*.svc
文件 - 在网站文件夹中添加适当的
web.config
以定义您的端点和服务配置等.
- compile your WCF service into a DLL (class library)
- create a website in IIS
- copy the WCF DLL's into the website's
.in
folder - create a
*.svc
file in that website - add an appropriate
web.config
in the website folder to define your endpoints and service configuration etc.
您的 WCF 服务现在可以通过网站的基地址以及 *.svc
文件的名称访问,例如
Your WCF service will now be reachable at the website's base address, plus the name of the *.svc
file, e.g.
http://myserver/someweb/Myservice.svc
你的 *.svc
看起来像这样:
Your *.svc
would look something like this:
<%@ ServiceHost Language="C#" Debug="true"
Service="WCF_Simple_Service.HelloIndigoService" %>
Service=
属性表示实现服务的类 - 使用其命名空间完全限定.
The Service=
attributes denotes the class implementing the service - fully qualified with its namespace.
选项 2 - 将内容放入 App_Code
- 在 IIS 中创建网站
- 将所有与 WCF 相关的
*.cs
文件直接放入.App_Code
文件夹 - 在该网站中创建一个
*.svc
文件 - 在网站文件夹中添加适当的
web.config
以定义您的端点和服务配置等.
- create a website in IIS
- put all your WCF related
*.cs
files directly into the.App_Code
folder - create a
*.svc
file in that website - add an appropriate
web.config
in the website folder to define your endpoints and service configuration etc.
您的 WCF 服务现在可以通过网站的基地址以及 *.svc
文件的名称访问,例如
Your WCF service will now be reachable at the website's base address, plus the name of the *.svc
file, e.g.
http://myserver/someweb/Myservice.svc
你的 *.svc
看起来像这样:
Your *.svc
would look something like this:
<%@ ServiceHost Language="C#" Debug="true"
Service="Service"
CodeBehind="~/App_Code/Service.cs" %>
一个简单的示例 web.config
可能如下所示:
A simple, sample web.config
might look something like this:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WithDebug">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="SimpleWCF.HelloIndigoService" behaviorConfiguration="WithDebug">
<endpoint
address=""
binding="basicHttpBinding"
contract="SimpleWCF.IHelloIndigoService" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
您基本上定义了 <service>
标记 - 再次:name=
表示实现服务的类 - 完全限定其命名空间.它必须至少包含一个端点——mex"端点是可选的——但非常有用,尤其是对于开发和测试.它允许客户端发现"服务并获取其服务描述,以便与它进行交互.
You basically define your <service>
tag - and again: the name=
denotes the class implementing the service - fully qualified with its namespace. It must contain at least one endpoint - a "mex" endpoint is optional - but very useful, especially for development and testing. It allows client to "discover" the service and get its service description so it can interface with it.
在 IIS 中部署您的服务后,您可以使用诸如 WCF 测试客户端随 WCF 一起免费提供,或 SoapUI 这是一个通用的SOAP 测试实用程序(提供免费版本供您使用).
Once your service is deployed in IIS, you can see it in action using a tool like the WCF Test Client that ships for free with WCF, or SoapUI which is a general-purpose SOAP testing utility (with a free edition for you to use).
这篇关于如何在我的网站中托管我的 WCF 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!