第一个WCF程序(最小实现)

环境:Microsoft Visual Studio 2017

注意事项:右键“以管理员身份”启动VS。

目标:实现一个加法运算,并在服务器端D盘新建一个文件夹。

一、服务器端创建WCF服务

1、右键启动VS;

2、“文件”-“新建”-“项目”,Visual C#->Windows经典桌面,右侧的“类库(.net Framework)”,新建一个项目“SytWCFService”;

3、右键“SytWCFService”->添加->新建项,选择“接口”;右键“Interface1”选择重命名,为“ISytService”;修改文件名为“ISytService”;修改为public interface;

4、添加引用“System.ServiceModel”:右键添加,引用;

5、完成以下代码(创建接口):

using System.ServiceModel;

namespace SytWCFService
{
/// <summary>
/// 定义服务契约:通过接口实现契约
/// </summary>
[ServiceContract(Name = "SytService")]
public interface ISytInterface
{
/// <summary>
/// 定义操作契约
/// </summary>
/// <param name="num1"></param>
/// <param name="num2"></param>
/// <returns></returns>
[OperationContract(Name = "AddOperation")]
double AddOperation(double num1, double num2);


[OperationContract(Name = "CreateDir")]
bool CreateDir(string dir);
}
}

6、实现接口:新建类SytService : ISytService,继承ISytService接口,在接口名上点左键,可以一键“实现接口”。

7、完成以下代码(实现接口):

扫描二维码关注公众号,回复: 7930683 查看本文章
using System.IO;


namespace SytWCFService

{
public class SytService : ISytService
{
public double AddOperation(double num1, double num2)
{
return num1 + num2;
}

public bool CreateDir(string dir)
{
if (!Directory.Exists(dir))//如果不存在就创建 dir 文件夹 
{
Directory.CreateDirectory(dir);
return true;
}
else
{
return false;
}
}
}
}

二、定义宿主,寄宿WCF服务

WCF服务典型的宿主包括以下四种:
"Self-Hosting" in a Managed Application(自托管宿主)
Managed Windows Services(Windows Services宿主)
Internet Information Services(IIS宿主)
Windows Process Activation Service(WAS宿主)

1、以自托管宿主的方式寄宿。

1.1 通过代码的方式配置WCF服务

略。

1.2 通过配置文件的方式配置WCF服务

(1)添加一个console控制台项目“ConsoleHosting”,完成以下代码:

using System.ServiceModel;
using SytWCFService;

namespace ConsoleHosting
{
    class Program
    {
        static void Main(string[] args)
        {
            //如果采用了配置文件的方式,服务寄宿代码将会得到极大的精简,只需包含下面几行代码:
            using (ServiceHost host = new ServiceHost(typeof(SytService)))
            {
                host.Open();
                Console.WriteLine("服务已启动");
                Console.Read();
            }
            //通过代码的方式配置WCF服务
            //using (ServiceHost host = new ServiceHost(typeof(SytService), new Uri("http://127.0.0.1:9099/CalculateWcfService")))
            //{
            //    BasicHttpBinding bind = new BasicHttpBinding
            //    {
            //        Name = "basichttpbinding"
            //    };
            //    host.AddServiceEndpoint(typeof(ICalculateService), bind, "CalculateWcfService");
            //    if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
            //    {
            //        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior()
            //        {
            //            HttpGetEnabled = true
            //        };
            //        host.Description.Behaviors.Add(behavior);
            //    }
            //    host.AddServiceEndpoint(typeof(IMetadataExchange), bind, "mex");
            //    if (host.State != CommunicationState.Opened)
            //    {
            //        host.Open();
            //    }

            //    Console.WriteLine("服务已启动");
            //    Console.Read();
            //}
        }
    }
}

(2)在“ConsoleHosting”项目中添加一个app.config应用程序配置文件。

猜你喜欢

转载自www.cnblogs.com/upcsyt/p/11906975.html