首页 技术 正文
技术 2022年11月21日
0 收藏 864 点赞 3,614 浏览 3168 个字

如果我们自己新建一个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的事件里添加如下代码:

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

注意本句:

  1. Host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), httpbinding, “http://localhost:8002”);
Host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), httpbinding, "http://localhost:8002");

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

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

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

  1. if (Host != null)
  2. {
  3. Host.Close();
  4. }
if (Host != null)
{
Host.Close();
}

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事件中填写如下代码:

  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. }
        private void button1_Click(object sender, EventArgs e)
{
using (ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client())
{
sc.Open();
MessageBox.Show(sc.GetData(10));
sc.Close();
}
}

5).生成,运行即可。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893