65.9K
CodeProject 正在变化。 阅读更多。
Home

简化 WCF 自我托管到控制台应用程序

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.97/5 (10投票s)

2014 年 6 月 9 日

CPOL

2分钟阅读

viewsIcon

71211

为了托管 Windows Communication Foundation 服务,我们通常需要一个托管进程、一个 ServiceHost 实例以及为 WCF 服务配置的端点。我们可以通过以下不同的方式托管 WCF 服务:在托管应用程序中/自托管控制台应用程序窗口中。

为了托管 Windows Communication Foundation 服务,我们通常需要一个托管进程、一个 ServiceHost 实例以及为 WCF 服务配置的端点。我们可以通过以下不同的方式托管 WCF 服务

  • 在托管应用程序中/自托管
    • 控制台应用程序
    • Windows/WPF 应用程序
    • Windows 服务
  • 在 Web 服务器上托管
    • IIS 6.0 (ASP.NET 应用程序仅支持 HTTP)
    • Windows 进程激活服务 (WAS),即 IIS 7.0 支持 HTTP、TCP、命名管道、MSMQ。

在本 WCF 教程中,重点是使用循序渐进的方法在控制台应用程序中自托管我们的 WCF 服务。在控制台应用程序中自托管 WCF 服务相对容易且灵活,因为我们可以通过编写几行代码来实现目的。让我们首先创建一个简单的 WCF 服务,即 StudentService,然后在控制台应用程序中托管它。StudentService 具有服务操作 GetStudentInfo,它将 StudentId 作为参数并返回学生姓名。

1. 创建 StudentService 类库

打开 Visual Studio 并创建一个新的类库项目,将其命名为“StudentService”并按“确定”按钮。

然后,右键单击项目并向此类库项目添加一个新的“WCF 服务”。

它会将服务契约 (IStudentService) 及其实现类 (StudentService) 添加到类库项目。它还会添加对 System.ServiceModel 的引用。

IStudentService 接口的代码如下

[ServiceContract] public interface IStudentService 
 {
     [OperationContract]
     string GetStudentInfo(int studentId);
 }

以下是 StudentService 实现类的代码

      public class StudentService : IStudentService
     {
         public string GetStudentInfo(int studentId)
         {
             string studentName = string.Empty;
             switch (studentId)
             {
                 case 1:
                     {
                         studentName = “Muhammad Ahmad”;
                         break;
                     }
                 case 2:
                     {
                         studentName = “Muhammad Hamza”;
                         break;
                     }
                 default:
                     {
                         studentName = “No student found”;
                         break;
                     }
             }
             return studentName;
         }
     }

2. 添加控制台应用程序

为了在控制台应用程序中托管此服务,让我们向此解决方案添加一个新的控制台应用程序项目“StudentHost”。

我们的控制台应用程序将引用

  • StudentService 类库
  • System.ServiceModel.

Reference to WCF Service

System.ServiceModel

在本文 WCF 教程的开头,我们讨论了托管 WCF 服务需要一个托管进程(即控制台应用程序)、服务主机(ServiceHost 类的一个实例)和一个或多个服务端点。Student host 应用程序的详细实现如下

class Program
    {
        static void Main(string[] args)
        {
            ServiceHost studentServiceHost = null;
            try
            {
                //Base Address for StudentService
                Uri httpBaseAddress = new Uri(“https://:4321/StudentService”);
                
                //Instantiate ServiceHost
                studentServiceHost = new ServiceHost(typeof(StudentService.StudentService),
                    httpBaseAddress);
 
                //Add Endpoint to Host
                studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService), 
                                                        new WSHttpBinding(), “”);            
 
                //Metadata Exchange
                ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
                serviceBehavior.HttpGetEnabled = true;
                studentServiceHost.Description.Behaviors.Add(serviceBehavior);

                //Open
                studentServiceHost.Open();
                Console.WriteLine(“Service is live now at : {0}”, httpBaseAddress);
                Console.ReadKey();                
            }

            catch (Exception ex)
            {
                studentServiceHost = null;
                Console.WriteLine(“There is an issue with StudentService” + ex.Message);
            }
        }
    }

现在,只需构建控制台应用程序并在将其设置为启动项目后运行它。您将看到以下屏幕,显示我们的自托管 WCF 服务正在运行。

Self Hosted in Console Application

在本 WCF 服务教程中,我们创建了一个 Windows Communication Foundation 服务并在控制台应用程序中托管了它。在 WCF 博客的后续文章中,我们将从客户端应用程序调用此自托管 WCF 服务。

文章 WCF 自托管在控制台应用程序中的简化方法 最先发表在 WCF 教程 上。

© . All rights reserved.