使用Winform程序作为WCF服务的宿主



如果我们自己新建一个WCF服务库,生成了dll文件。那我们需要创建一个宿主程序,在本例中我们新建一个Winform程序作为WCF的宿主程序。

在网上很多教程里对创建过程写的很模糊,错误也很多。本文是作者在尝试了网上各种失败方法之后,经过自己的改正,总结出的可以正确运行的解决方案。


 1. 创建wcf服务库。

打开vs, 新建一个 WCF服务库。 什么都不用改,直接生成。 此时会在bin目录下生成一个dll文件(默认名WcfServiceLibrary1.dll)。


2. 创建宿主程序。

1). 打开vs,新建一个Windows窗体应用程序。

2). 添加引用,System.ServiceModel和 刚刚生成的WcfServiceLibrary1.dll。

3). 创建一个button,在button.click的事件里添加如下代码:

[csharp]  view plain  copy
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     if (Host == null)  
  4.     {  
  5.         Host = new ServiceHost(typeof(WcfServiceLibrary1.Service1));  
  6.   
  7.         System.ServiceModel.Channels.Binding httpbinding = new BasicHttpBinding();  
  8.   
  9.         Host.AddServiceEndpoint(typeof(WcfServiceLibrary1.Service1), httpbinding, "http://localhost:8002");  
  10.         if (Host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>() == null)  
  11.         {  
  12.             ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  
  13.             behavior.HttpGetEnabled = true;  
  14.   
  15.             behavior.HttpGetUrl = new Uri("http://localhost:8002/Service1");  
  16.             Host.Description.Behaviors.Add(behavior);  
  17.   
  18.             Host.Open();  
  19.   
  20.             MessageBox.Show("OK");  
  21.         }  
  22.   
  23.   
  24.     }  
  25.   
  26. }  



注意本句:
[csharp]  view plain  copy
  1. Host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), httpbinding, "http://localhost:8002");  


.IService1 为网络教程中出错经作者修改的部分,此处如果写成Service1的话会报如下的错:

协定类型WcfServiceLibrary1.Service1不具有ServiceContractAttribute特性。若要定义有效协定(协定接口或服务类)必须具有ServiceContractAttribute特性。


4). 在窗口关闭的事件里添加如下代码:

[csharp]  view plain  copy
  1. if (Host != null)  
  2. {  
  3.     Host.Close();  
  4. }  

5). 生成,运行。


3.  创建客户端程序。

1). 打开vs,新建一个Windows窗体应用程序。

2). 右键点击引用,然后选择添加服务引用,在地址栏中输入 http://localhost:8002/Service1 ,即刚刚代码中的behavior的uri,注意不是http://localhost:8002/。

3). 点击前往,然后点确定即可。


注意:在网上的好些教程里都写的是在宿主程序用用WCF配置工具生成app.config然后运行宿主程序,那样生成的app.config是有问题的,在客户端程序引用的时候会报错。错误提示:

下载“http://localhost:8082/wcf2”时出错。
请求因 HTTP 状态 400 失败: Bad Request。
元数据包含无法解析的引用:“http://localhost:8082/wcf2”。
服务 http://localhost:8082/wcf2 不支持内容类型 application/soap+xml; charset=utf-8。客户端和服务绑定可能不匹配。
远程服务器返回错误: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.。
如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。

此问题绝对会发生,但那些教程里并没有指出错误并告知解决办法。困扰我许久之后我终于发现本文中的方法。


4). 创建一个button,button的click事件中填写如下代码:

[csharp]  view plain  copy
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     using (ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client())  
  4.     {  
  5.         sc.Open();  
  6.         MessageBox.Show(sc.GetData(10));  
  7.         sc.Close();  
  8.     }  
  9. }  

5).生成,运行即可。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值