首先说一个服务引用不成功的解决办法:
需要把服务端配置文件中的Binding换成:
<endpoint address="" binding="BasicHttpBinding" contract="WcfServiceLibrary1.IService1">
或:
<endpoint address="" binding="wsDualHttpBinding" contract="WcfServiceLibrary1.IService1">
下面是一个wcf的简单示例:
服务契约代码:
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IUser”。
[ServiceContract(Namespace = "localhost:60992/User",
CallbackContract = typeof(ICallback))]
public interface IUser
{
[OperationContract(IsOneWay=true)]
void Add(double X, double y);
}
public interface ICallback
{
[OperationContract(IsOneWay = true)]
void DisplayResult(double x, double y, double result);
}
}
实现服务契约代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“User”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 User.svc 或 User.svc.cs,然后开始调试。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class User : IUser
{
public void Add(double x, double y)
{
double result = x + y;
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
callback.DisplayResult(x, y, result);
}
}
}
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime appRequestQueueLimit="100" executionTimeout="900" useFullyQualifiedRedirectUrl="false" maxRequestLength="102400" shutdownTimeout="900" targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfService.User">
<!--<endpoint address="http://localhost:60992/User" behaviorConfiguration="WcfService.Service1AspNetAjaxBehavior"
binding="webHttpBinding" contract="WcfService.IUser" />-->
<endpoint address="" binding="wsDualHttpBinding" contract="WcfService.IUser"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WcfService.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"
multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。
在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
此配置注意有事后出错请修改红色部分。
客户端代码:
首先在客户端使用服务引用之前要想成功使用回调必须实现回调函数:
class CallbackHandler : ServiceReference1.IUserCallback
{
public void DisplayResult(double x, double y, double result)
{
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", x, y, result);
}
}
实现回到函数之后使用能够控制到调用服务使用:
static void Main(string[] args)
{
InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
ServiceReference1.UserClient c = new ServiceReference1.UserClient(instanceContext);
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.WriteLine();
c.Add(1.1, 2.2);
Console.ReadLine();
}
最后附带源码下载地址:https://pan.baidu.com/s/1hr6zPCK