WCF服务使用(IIS+Http)和(Winform宿主+Tcp)两种方式进行发布
[ServiceContract]
public interface IPeopleInfo
{
[OperationContract]
int GetAge(string name);
}
数据契约:
[DataContract]
public class People
{
public string Name;
public int Age;
}
服务实现:
public class PeopleInfo : IPeopleInfo
{ public int GetAge(string name)
{
List<People> peopleList = DataFactory.GetPeopleList();
return peopleList.Find(t =>t.Name==name).Age ;
}
}
辅助类:
public class DataFactory
{
public static List<People> GetPeopleList()
{
List<People> peopleList = new List<People>();
peopleList.AddRange(new People[] {
new People{Name="tjm",Age=},
new People{Name="lw",Age=},
new People{Name="tj",Age=},
});
return peopleList;
}
}
WCF部署IIS出现“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射”的解决办法
管理员身份运行C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe -i
HTTP 错误 404.3 - Not Found,
使用管理员注册:C:\Windows\system32>"C:\Windows\Microsoft.NET\Framework\v4.0.30319\ServiceModelReg.exe" -r
Microsoft(R) WCF/WF 注册工具版本 4.5.0.0
版权所有(C) Microsoft Corporation。保留所有权利。
用于管理一台计算机上 WCF 和 WF 组件的安装和卸载的管理实用工具。
[错误]此 Windows 版本不支持此工具。管理员应改为使用“打开或关闭 Windows 功能”对话框或 dism 命令行工具来安装/卸载 Windows Communication Foundation 功能。
根据错误的提示,去控制面板->程序->启用或关闭Windows功能,如下图

把我画红线的框内的复选框全部勾选,点击确定,然后再在iis中再进行浏览就能够找到发布后的WCF服务了,原因应该是我没有安装WCF服务的组件而导致的吧。在浏览器中浏览服务如下。

在window功能中卸载iis和WCF服务,然后再重新安装配置。
首先建立一个Winform项目,界面如下。

注意:在部署之前需要把第一步中编写服务的dll引用进来。
ServiceHost host;
private void btnStart_Click(object sender, EventArgs e)
{
//使用代码绑定
Uri tcpa = new Uri("net.tcp://172.21.212.54:8090/peopleinfo");
host = new ServiceHost(typeof(FirstWCFService.PeopleInfo), tcpa);
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
NetTcpBinding tcpb = new NetTcpBinding();
host.Description.Behaviors.Add(mBehave);
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
host.AddServiceEndpoint(typeof(FirstWCFService.IPeopleInfo), tcpb, tcpa); host.Open();
this.btnStart.Enabled = false;
this.label1.Text = "Service Running";
} private void btnStop_Click(object sender, EventArgs e)
{
if (host != null)
{
host.Close();
this.label1.Text = "Service Closed";
this.btnStart.Enabled = true;
}
}
2)使用配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="FirstWCFService.PeopleInfo">
<!--客户端用来解释服务,这个端点如果缺少服务会发布失败-->
<endpoint contract="IMetadataExchange"
binding="mexTcpBinding" address ="net.tcp://172.21.212.54:8090/peoleinfo/mex"></endpoint>
<!--提供服务的端点-->
<endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding"
contract="FirstWCFService.IPeopleInfo"> </endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
虽然是用来配置文件,但是开启服务的代码还是需要写的。代码如下。
ServiceHost host;
private void btnStart_Click(object sender, EventArgs e)
{
host = new ServiceHost(typeof(FirstWCFService.PeopleInfo));
host.Open();
this.btnStart.Enabled = false;
this.label1.Text = "Service Running";
} private void btnStop_Click(object sender, EventArgs e)
{
if (host != null)
{
host.Close();
this.label1.Text = "Service Closed";
this.btnStart.Enabled = true;
}
}
运行程序,点击Start按钮,到此使用Winform作为宿主来发布WCF服务已经成功了。



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IPeopleInfo" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_IPeopleInfo">
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://172.21.212.54/PeopleInfo.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IPeopleInfo" contract="HttpService.IPeopleInfo"
name="BasicHttpBinding_IPeopleInfo" />
<endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IPeopleInfo" contract="TcpService.IPeopleInfo"
name="NetTcpBinding_IPeopleInfo" />
</client>
</system.serviceModel>
</configuration>
调用服务操作的代码非常简单,就类似于操作本地的类一样,代码如下:
private void bntGet_Click(object sender, EventArgs e)
{
string name = this.txtName.Text;
int age;
if (rdbHttp.Checked)
{
HttpService.PeopleInfoClient httpClient = new HttpService.PeopleInfoClient();
age = httpClient.GetAge(name);
}
else
{
TcpService.PeopleInfoClient tcpClient = new TcpService.PeopleInfoClient();
age = tcpClient.GetAge(name);
}
this.txtAge.Text = age.ToString();
}
到此,wcf的客户端编写完成,结果如下。


System.ServiceModel.Security.SecurityNegotiationException: 服务器已拒绝客户端凭据。 --->
System.Security.Authentication.InvalidCredentialException: 服务器已拒绝客户端凭据。 --->
System.ComponentModel.Win32Exception: 登录没有成功
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="FirstWCFService.PeopleInfo">
<!--客户端用来解释服务,这个端点如果缺少服务会发布失败-->
<endpoint contract="IMetadataExchange"
binding="mexTcpBinding" address ="net.tcp://172.21.212.54:8090/peoleinfo/mex"></endpoint>
<!--提供服务的端点-->
<endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding"
contract="FirstWCFService.IPeopleInfo" bindingConfiguration="NoSecurity">
</endpoint>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="NoSecurity">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
设置完成之后,重新开启服务。然后在客户端删除winform宿主发布的服务,并且再重新添加,以便在配置文件中重新生成访问tcp方式的服务端点的配置。更新后的配置文件如下,红色加粗部分为更新的。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IPeopleInfo" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_IPeopleInfo">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://172.21.212.54/PeopleInfo.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IPeopleInfo" contract="HttpService.IPeopleInfo"
name="BasicHttpBinding_IPeopleInfo" />
<endpoint address="net.tcp://172.21.212.54:8090/peoleinfo" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IPeopleInfo" contract="TcpService.IPeopleInfo"
name="NetTcpBinding_IPeopleInfo" />
</client>
</system.serviceModel>
</configuration>
WCF服务使用(IIS+Http)和(Winform宿主+Tcp)两种方式进行发布的更多相关文章
- c# winform播放声音的两种方式
axWindowsMediaPlayer1.settings.setMode("", true); axWindowsMediaPlayer1.URL = "skcg.m ...
- WCF 一步一步 发布 WCF服务 到 IIS (图)
WCF 一步一步 发布 WCF服务 到 IIS (图) 使用VS自带的WCFSVCHost(WCF服务主机)发布WCF服务,时刻开发人员测试使用. 下面我们来看一下如何在IIS中部发布一个WCF服务. ...
- .Net WCF服务部署IIS详细解析
官方解析:Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台.整合了原有的windows通 ...
- C#动态调用WCF接口,两种方式任你选。
写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...
- 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式
一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...
- 不停止MySQL服务增加从库的两种方式
不停止MySQL服务增加从库的两种方式 转载自:http://lizhenliang.blog.51cto.com/7876557/1669829 现在生产环境MySQL数据库是一主一从,由于业务量访 ...
- XFire构建服务端Service的两种方式(转)
XFire构建服务端service的两种方式,一是用xfire构建,二是和spring集成构建. 一,xifre构建,确保把xfire的jar包导入到工程中或classpath. 1,service的 ...
- XFire构建服务端Service的两种方式
1.原声构建: 2.集成spring构建 http://blog.csdn.net/carefree31441/article/details/4000436XFire构建服务端Service的两种方 ...
- 不停止MySQL服务增加从库的两种方式【转载】
现在生产环境MySQL数据库是一主一从,由于业务量访问不断增大,故再增加一台从库.前提是不能影响线上业务使用,也就是说不能重启MySQL服务,为了避免出现其他情况,选择在网站访问量低峰期时间段操作. ...
随机推荐
- 匹配img标签的正则表达式
$preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';//匹配img标签的正则表达式 preg_match_all($ ...
- webpack快速入门——插件配置:HTML文件的发布
1.把dist中的index.html复制到src目录中,并去掉我们引入的js 2.在webpack.config.js中引入 const htmlPlugin = require('html-web ...
- tomcat安装配置常见问题详解
历经波折,终于把tomcat装好了.记录下过程供自己和后来的初学者参考吧! 本文先后介绍了tomcat的下载安装方法.安装和启动不成功的常见原因 以及启动tomcat后如何配置上下文. 一.下载安装 ...
- 双向一对一映射@OneToOne
双向一对一的实例我已经上传到GitHub,entrance项目上了,感兴趣的可以下载下来跑跑,这里讲两个在运行过程中遇到的问题. 问题一:上一篇博客的最后我留下了问题.一对一关联查询注解@OneToO ...
- 深入理解Java接口和抽象类
对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的时候会以为它们可以随意互换使用, ...
- numpy的ravel()和flatten()函数比较
功能 两个函数的功能都是将多维数组降为一维. 用法 import numpy as np arr = np.array([[1, 2],[3, 4]]) arr.flatten() arr.ravel ...
- 聚类系数可变无标度网络模型Holme-Kim HK模型
# -*- coding: cp936 -*- import random import networkx as nx from networkx.generators.classic import ...
- SpringMVC+AJAX+JSON
在做一个ajax发送json到springmvc的控制层,控制层的对象中有一个List集合,ajax调用总是报415错误.发现了一个一直没有注意到的问题,借机记录一下. (细节部分都忽略了,在最后的d ...
- mac 系统安装VM虚拟机打开时报错,提示不是虚拟磁盘的解决方式。
最近刚买的苹果系统,不太会用,装了个虚拟机vmware fusion,好不容易把需要的软件装好,然后不知道是我操作了哪里,今天再次打开虚拟机的时候打不开了,报错提示找不到磁盘文件(虚拟磁盘-00000 ...
- WebBench压力测试工具
Webbench是有名的网站压力测试工具,它是由 Lionbridge公司(http://www.lionbridge.com)开发. Webbech能测试处在相同硬件上,不同服务的性能以及不同硬件上 ...