// 版本 .NET Framework 4.7.2
// 新建 WCF 服务应用程序
// interface IService1 中添加
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData?value={value}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
// class Service1 中添加
public string GetData(int value)
{
return MessageService.Instance.SendMsg(value);
}
// 添加 MessageService.cs 文件
public class MessageService
{
private static MessageService _instance = null;
public static MessageService Instance
{
get
{
if (_instance == null)
_instance = new MessageService();
return _instance;
}
}
public delegate string DelegateMessage(int cs);
public DelegateMessage delegateMessage;
public string SendMsg(int cs)
{
return delegateMessage(cs);
}
}
// 新建 WPF 项目
// 引入 WCF 服务
public MainWindow()
{
InitializeComponent();
System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(WcfService1.Service1), new Uri("http://ip:端口"));
System.ServiceModel.WebHttpBinding webHttpBinding = new System.ServiceModel.WebHttpBinding();
host.AddServiceEndpoint(typeof(WcfService1.IService1), webHttpBinding, "");
host.Description.Behaviors.Add(new System.ServiceModel.Description.ServiceMetadataBehavior() {
HttpGetEnabled = true });
host.Description.Endpoints[0].EndpointBehaviors.Add(new WebHttpBehavior());
if (httpHost.State != CommunicationState.Opened) host.Open();
MessageService.Instance.delegateMessage += InitGetMsg;
}
string InitGetMsg(int cs)
{
// TODO 具体业务
return "测试" + cs;
}
WPF 内置WCF服务配置
猜你喜欢
转载自blog.csdn.net/weixin_45381071/article/details/135951030
今日推荐
周排行