C#使用WCF 搭建本地服务(并且跨域)无需IIS和Web容器

本文档详细介绍了如何使用C#通过WCF创建本地服务,实现Web端与C#窗体程序的通讯。内容涵盖工程创建、WCF库引入、接口定义、接口实现、服务管理类创建、配置文件设置以及界面交互。同时,解决了跨域问题,提供了项目下载链接。
摘要由CSDN通过智能技术生成

接到有一个需求,需要web 端和我们的应用程序通讯,因为我们的客户端是C# 窗体程序,接到这个需求一下想到可以使用WCF 建立通讯,在过程中,涉及到跨域问题,研究了一番得以解决,当然还有其它的方式不如建立本地socket 服务一样,但我wcf 程序更加清晰,所以在此记录。

1. 建立工程

我这里使用VS2019 建立的窗体程序
在这里插入图片描述
在这里插入图片描述

2 .引入对应WCF 的Dll

右键引用 选择程序集

在这里插入图片描述
在这里插入图片描述

3. 边写wcf 接口定义

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1.WCFService
{
    /// <summary>
    /// author  : Mcally
    /// @date   : 2021年6月10日16:56:08
    /// @des    :  服务接口定制类
    /// </summary>


    [ServiceContract(Namespace = "http://www.mcally.net/")]
    partial  interface IWebPrintService
    {
        /// <summary>
        /// 获取本地Ip示例
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, 
            ResponseFormat = WebMessageFormat.Json, 
            UriTemplate = "/getIp", Method = "POST")]
        GetIP GetLocalHostIps();

        /// <summary>
        /// 下载一个文件示例
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json,
          UriTemplate = "/dowload", Method = "GET")]
        Stream DowloadFile();
    }
}

4. 实现上面wcf 的接口定义


using System;
u
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值