WCF服务的创建和发布到IIS

it2022-05-08  6

一、 WCF服务的创建

有两种创建方式:

1.WCF服务库

2.WCF服务应用程序

如下图所示:

 

这里选择WCF服务库。注意事项:

1.WCF服务库是一个类库项目,这里选择.net 3.5版本(版本高低可以会有一些冲突)。

2.因为是类库,所以配置文件是App.config,要发布到IIS,就必须将其改名为web.config。用原来的IService接口和实现Service范例。然后在添加Web.config配置:

文件名:Web.config :

<?xml version="1.0"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> <services> <service behaviorConfiguration="ServiceBehavior" name="WcfServiceLibrary1.Service1"> <endpoint binding="wsHttpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1"/> </service> </services> </system.serviceModel> </configuration>

特别主要要添加:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

否则可能在

三、从IIS宿主引用WCF服务添加引用WCF服务的时候无法找到服务,出现如下错误信息:

下载“http://127.0.0.1:7293/WCFServerRefWCFLib.svc”时出错。请求因 HTTP 状态 404 失败: Not Found。元数据包含无法解析的引用:“http://127.0.0.1:7293/WCFServerRefWCFLib.svc”。没有终结点在侦听可以接受消息的 http://127.0.0.1:7293/WCFServerRefWCFLib.svc。这通常是由于不正确的地址或者 SOAP 操作导致的。如果存在此情况,请参见 InnerException 以了解详细信息。远程服务器返回错误: (404) 未找到。如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。

 

3.添加svc文件。   选择添加类,找不到svc类型的文件模板。可以直接输入”文件名+.svc“,例如WCFServerUseWCF.svc,点“确定”创建一个svc格式的文件。清除自动生成的WCFServerUseWCF.svc的内容。添加如下代码:

文件名:WCFServerUseWCF.svc

<%@ ServiceHost Language="C#" Service="WcfServiceLibrary1.Service1"%>

 

还有一种方式不是直接让IIS访问WCF类库项目,而是新建一个网站,方法如下:

在解决方案节点--右键-新建网站--在弹出框中选择,WCF服务。如下图:

 

然后添加对WCF类库的应用。引用完成之后会在Bin中看到引用了WCF类库的dll,如下图所示:

然后是添加web.config和.svc文件和内容,与上面完全一样的做法。

 

二、IIS作为宿主,发布WCF服务

确保已经安装IIS!

 步骤1:

"我的电脑"右键--“管理”--“服务和应用程序”--“Interner信息服务(IIS)管理器”--“网站”右键,添加一个网站。如下图:

 

步骤2:

启动网站和

浏览http://127.0.0.1:7293/WCFServerUseWCFLib.svc

如果发布成功,会在浏览器中有如下画面:

 

点击:http://127.0.0.1:7293/WCFServerUseWCFLib.svc?wsdl,会在浏览器中有如下画面:

 

 

三、从IIS宿主引用WCF服务

步骤1:

        创建一个项目,这里选择WPF应用程序。

步骤2:

       右键“WPF应用程序”-添加服务引用 --地址,输入:http://127.0.0.1:7293/WCFServerUseWCFLib.svc--点击“前往”按钮,发现WCF服务.重命名服务命名空间(这里命名为:WCFServerUseWCFLib)。

如下图所示配置:

点击“确定”按钮;

 引用成功后,App.config 文件自动添加了WCF Client端的配置,如下所示:

文件名:App.config

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" /> </security> </binding> <binding name="WSHttpBinding_IService11" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://127.0.0.1:7293/WCFServerUseWCFLib.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="WCFServerUseWCFLib.IService1" name="WCFServerUseWCFLibIService"> <identity> <servicePrincipalName value="host/weiMe-PC" /> </identity> </endpoint> <endpoint address="http://127.0.0.1:7294/WCFServerRefWCFLib.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService11" contract="WCFServerRefWCFLib.IService1" name="WCFServerRefWCFLibIService"> <identity> <servicePrincipalName value="host/weiMe-PC" /> </identity> </endpoint> </client> </system.serviceModel> </configuration> View Code

 

步骤3:

导入命名空间:

using WpfApplication1.WCFServerUseWCFLib;

调用WCF服务(这里调用方法 :

string GetData(int)

private void BtnWCFLibToIIS_OnClick(object sender, RoutedEventArgs e) { using (WCFServerUseWCFLib.Service1Client proxy = new WCFServerUseWCFLib.Service1Client()) { MessageBox.Show(this.BtnWCFWebRefWCFLibTOIIS.Content.ToString() + proxy.GetData(2)); } }

 

 

如果要客户端要使用异步编程,可以让客户端代理支持异步编程,做法是:

1.右键WCF服务的引用---选择“配置服务引用”。

 

2.在弹出的对话框中,勾选“生成异步操作”--点击“确定”按钮。

 

 

四、解决方案

 

 【The End】

 

 

 

 

 

转载于:https://www.cnblogs.com/easy5weikai/p/3341147.html

相关资源:WCF服务端运行时架构体系详解

最新回复(0)