WCF路由服务
代码下载: 链接:https://pan.baidu.com/s/1i76Ht0lMWmosaCrDjaA2cA 密码:muj1
1.新建类库
Service.Interface
using System.ServiceModel;
namespace Artech.RoutingServiceDemo.Service.Interface
{
[ServiceContract(Namespace="http://www.artech.com/")]
public interface IHello
{
[OperationContract]
string SayHello(string userName);
}
[ServiceContract(Namespace = "http://www.artech.com/")]
public interface IGoodbye
{
[OperationContract]
string SayGoodbye(string userName);
}
}
.新建web项目
TestService
using Artech.RoutingServiceDemo.Service.Interface;
namespace Service
{
public class HelloService: IHello
{
public string SayHello(string userName)
{
return string.Format("Hello, {0}", userName);
}
}
public class GoodbyeService : IGoodbye
{
public string SayGoodbye(string userName)
{
return string.Format("Goodbye, {0}", userName);
}
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web; namespace TestWcf
{
[ServiceContract]
public interface IService
{
[OperationContract]
string DoWork();
}
public class Service2 : IService
{
public string DoWork()
{
return "你妹!";
}
}
/// <summary>
/// 用于调用服务的类
/// </summary>
public class MyClient : ClientBase<IService>, IService
{
public MyClient(Binding binding, EndpointAddress edpAddress)
: base(binding, edpAddress)
{ } /// <summary>
/// 调用服务端函数
/// </summary>
/// <returns></returns>
public string DoWork()
{ return base.Channel.DoWork();
} }
}
.testservice的web.config
<system.serviceModel>
<!--添加此节点,否则出现405错误-->
<bindings>
<wsHttpBinding>
<binding name="NoneSecurity" maxBufferPoolSize="" maxReceivedMessageSize="" useDefaultWebProxy="false">
<readerQuotas maxStringContentLength="" maxArrayLength=""/>
<security mode="None"/>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="routingBehavior">
<routing filterTableName="greetingFilterTable"/>
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="TestWcf.Service2" behaviorConfiguration="metadataBehavior">
<endpoint address="" binding="wsHttpBinding" contract="TestWcf.IService" bindingConfiguration="NoneSecurity"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service name="Service.HelloService">
<endpoint binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IHello"/>
</service>
<service name="Service.GoodbyeService">
<endpoint binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IGoodbye"/>
</service>
<service behaviorConfiguration="routingBehavior" name="System.ServiceModel.Routing.RoutingService">
<endpoint binding="ws2007HttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter"/>
</service>
</services>
<client>
<endpoint name="helloService" address="http://localhost:65515/WcfService/HelloService.svc" binding="ws2007HttpBinding" contract="*"/>
<endpoint name="goodbyeService" address="http://localhost:65515/WcfService/GoodbyeService.svc" binding="ws2007HttpBinding" contract="*"/>
</client>
<routing>
<filters>
<filter name="Address4HelloService" filterType="EndpointAddress" filterData="http://localhost:65515/WcfService/HelloService.svc"/>
<filter name="Address4GoodbyeService" filterType="EndpointAddress" filterData="http://localhost:65515/WcfService/GoodbyeService.svc"/>
</filters>
<filterTables>
<filterTable name="greetingFilterTable">
<add filterName="Address4HelloService" endpointName="helloService"/>
<add filterName="Address4GoodbyeService" endpointName="goodbyeService"/>
</filterTable>
</filterTables>
</routing>
<!--无svc文件wcf服务激活-->
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="Service2.svc" service="TestWcf.Service2"/>
<add relativeAddress="WcfService/HelloService.svc" service="Service.HelloService"/>
<add relativeAddress="WcfService/GoodbyeService.svc" service="Service.GoodbyeService"/>
<!--, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35-->
<!--System.ServiceModel.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35-->
<add relativeAddress="WcfService/GrettingService.svc" service="System.ServiceModel.Routing.RoutingService, System.ServiceModel.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
.新建testwcfweb项目
using Artech.RoutingServiceDemo.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestWcf; namespace TestWcf
{
public partial class TestForm : System.Web.UI.Page
{ public void testnosvc()
{
EndpointAddress edpHttp = new EndpointAddress("http://localhost:65515/Service2.svc");
MyClient client = new MyClient(new WSHttpBinding(SecurityMode.None), edpHttp); Response.Write(client.DoWork()+"<br/>"); //Console.ReadKey();
}
public void test2()
{
using (ChannelFactory<IHello> channelFactoryHello = new ChannelFactory<IHello>("helloService"))
using (ChannelFactory<IGoodbye> channelFactoryGoodbye = new ChannelFactory<IGoodbye>("goodbyeService"))
{
IHello helloProxy = channelFactoryHello.CreateChannel();
IGoodbye goodbyeProxy = channelFactoryGoodbye.CreateChannel();
Response.Write(helloProxy.SayHello("Zhang San")+"<br/>");
Response.Write(goodbyeProxy.SayGoodbye("Li Si")+ "<br/>");
}
}
protected void Page_Load(object sender, EventArgs e)
{
testnosvc();
test2();
}
}
}
.testwcf的web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication18-20180807095436.mdf;Initial Catalog=aspnet-WebApplication18-20180807095436;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
<add namespace="Microsoft.AspNet.Identity" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls>
</pages>
<membership>
<providers>
<!--
已在此模板中禁用 ASP.NET 成员身份。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
-->
<clear />
</providers>
</membership>
<profile>
<providers>
<!--
已在此模板中禁用 ASP.NET 成员身份配置文件。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
-->
<clear />
</providers>
</profile>
<roleManager>
<!--
已在此模板中禁用 ASP.NET 成员身份角色。请访问以下链接 http://go.microsoft.com/fwlink/?LinkId=301889,以了解此模板中的 ASP.NET 成员身份支持
-->
<providers>
<clear />
</providers>
</roleManager>
<!--
如果要部署到具有多个 Web 服务器实例的云环境,
则应将会话状态模式从 "InProc" 更改为“自定义”。此外,
还应将名为 "DefaultConnection" 的连接字符串更改为连接到
SQL Server (包括 SQL Azure 和 SQL Compact)实例,而不是连接到 SQL Server Express 实例。
-->
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom> <!--wcf配置-->
<system.serviceModel>
<!--添加此节点,否则出现405错误-->
<bindings>
<wsHttpBinding>
<binding name="NoneSecurity"
maxBufferPoolSize="" maxReceivedMessageSize="" useDefaultWebProxy="false">
<readerQuotas maxStringContentLength="" maxArrayLength=""/>
<security mode="None"/>
</binding>
</wsHttpBinding>
</bindings> <behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior> <behavior name="routingBehavior">
<routing filterTableName="greetingFilterTable"/>
</behavior>
</serviceBehaviors> <endpointBehaviors>
<behavior>
<clientVia viaUri="http://localhost:65515/WcfService/GrettingService.svc"/>
</behavior>
</endpointBehaviors> </behaviors> <!--<protocolMapping>
<add binding="wsHttpBinding" scheme="http" />
</protocolMapping>--> <services>
<!--<service behaviorConfiguration="metadataBehavior" name="TestWcf.Service2">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="NoneSecurity"
contract="TestWcf.IService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>-->
</services> <client>
<endpoint name="helloService" address="http://localhost:65515/WcfService/HelloService.svc" binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IHello"/>
<endpoint name="goodbyeService" address="http://localhost:65515/WcfService/GoodbyeService.svc" binding="ws2007HttpBinding" contract="Artech.RoutingServiceDemo.Service.Interface.IGoodbye"/>
</client> <!--无svc文件wcf服务激活-->
<serviceHostingEnvironment>
<serviceActivations>
<!--<add relativeAddress="Service2.svc" service="TestWcf.Service2"/>--> </serviceActivations>
</serviceHostingEnvironment> </system.serviceModel>
</configuration>
WCF路由服务的更多相关文章
- WCF Routing 服务
WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发,能对服务做负载功能: 二.WC ...
- WCF Routing服务,负载均衡
WCF4.0支持路由机制,通过RoutingService实现请求分发.拦截处理. 一.应用场景 1.暴露一个endpoint在外网,其余服务部署于内网: 2.请求分发,能对服务做负载功能: 二.WC ...
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
- 如何:加载分页结果(WCF 数据服务)
WCF 数据服务 允许数据服务限制单个响应源中返回的实体数.在此情况下,源中的最后一项包含指向下一页数据的链接.通过调用执行 DataServiceQuery 时返回的 QueryOperationR ...
- 微软开源 WCF 分布式服务框架,并入 .NET 基金会项目
微软北京时间2015.5.20 在其 .NET Foundation GitHub 开源项目页中开放了 WCF 分布式服务框架的代码.WCF突然之间成为一个热门话题,在各大网站上都有不同的报道:dot ...
- 一个通过JSONP跨域调用WCF REST服务的例子(以jQuery为例)
JSONP(JSON with Padding)可以看成是JSON的一种“使用模式”,用以解决“跨域访问”的问题,这篇简单的文章给出一个简单的例子用于模拟如何通过jQuery以JSONP的访问调用一个 ...
- WCF服务与WCF数据服务的区别
问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data Se ...
- WCF 数据服务 4.5
.NET Framework 4.5 其他版本 WCF 数据服务(以前称为"ADO.NET Data Services")是 .NET Framework 的一个组件.可以使用此组 ...
- IIS上发布WCF发布服务,访问不到
1 环境是IIS7,发布WCF发布服务,访问不到. 一种原因站点自动生成“程序应用池”和站点的Framwork版本不一致. 解决的办法:新建一个“程序应用池”,然后站点指向这个新建的“程序应用池”
随机推荐
- MySQL 事务 隔离级别
前两天面试,问到了四种隔离级别,当时觉得大多数数据库都为read committed,结果没想到mysql是个例外.在此做一下隔离级别和各种数据库锁的使用. 首先说一下ACID四大特性: 四大特性 ...
- git桌面工具下载git源码
第一步,登陆githup,搜索自己需要查看的代码. 并复制clone url. 第二步,复制下载资源:选择菜单--FILE--CLONE
- 关于解决java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoader问题
解决方案: 其实是你的jar文件没有同步发布到自己项目的lib目录中 (如果是用Maven进行构建的话) 可以试试 下面的办法 –rebuild下project就可以了 项目点击右键 点击 Prope ...
- ASP.NET 在请求中检测到包含潜在危险的数据,因为它可能包括 HTML 标记或脚本
<textarea><%=Server.HtmlEncode(strContent)%></textarea> 转载:https://www.cnblogs.com ...
- defer和async的详细区别
看过javascript高级程序设计的人,在javascript高级程序设计里,应该看到了介绍了有关defer和async的区别,可是比较浅显,而且也说得不是很清楚.下面我们来通过图片来详细了解下df ...
- Kubernetes 之上的架构应用
规划并运转一个兼顾可扩展性.可移植性和健壮性的运用是一件很有应战的事情,尤其是当体系杂乱度在不断增长时.运用或体系 本身的架构极大的影响着其运转办法.对环境的依靠性,以及与相关组件的耦合强弱.当运用在 ...
- int 和 Integer 的区别
1.两个New生成的Integer 永远不相等,因为他们的内存地址不相等 2.如果一个是New生成的Integer 另一个是通过赋值生成的话,如果值相等那么他们相等,因为这时Integer会通过自动拆 ...
- 定义一个servlet用于处理所有外部接口类 架构思路
架构思路”: 所有外部URL访问请求(对外提供的接口)全部交给intServiceServlet处理, 然后servlet调用BPO通过URL中的命名去寻找相应的javaBean.接口BO,然后接口B ...
- Lua的闭包详解(终于搞懂了)
词法定界:当一个函数内嵌套另一个函数的时候,内函数可以访问外部函数的局部变量,这种特征叫做词法定界 table.sort(names,functin (n1,n2) return grades[n1] ...
- HDU 1166 敌兵布阵(线段树单点更新,区间查询)
描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...