从类库转换为WCF需要采取哪些步骤?

我创建了一个项目作为类库。 现在我需要将它变成WCF。 我可以创建一个WCF项目,但我想避免与TFS一起大惊小怪。 我已经完成了App.config并将/client:”wcfTestClient.exe“行添加到了命令行参数中。 但是,它似乎还有其他东西在推出主机时缺失。

我发现以下内容与您要实现的目标相反,即将服务库更改为控制台应用程序。

无法从VS中的设置屏幕编辑csproj文件中的某些设置,以将类库转换为WCF服务库,您需要将以下内容添加到项目文件中

将以下内容添加到第一个PropertyGroup [这些是C#WCF项目的guids]

 {3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 

有关ProjectTypeGuids的更多信息,请参见此处

您可能还需要在下面添加以下行:

 /client:"WcfTestClient.exe" 

但最终需要手动插入PropertyTypeGuids才能让VS将项目识别为WCF服务库项目。

这是我将类库转换为WCF REST应用程序所必须做的。

1)修改.csproj文件并将以下两行添加到.csproj文件中的第一个PropertyGroup元素。

 {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} false 

2) 添加到以导入Microsoft.WebApplication.targets文件

  

3)将以下代码添加到标记之前的文件末尾。

     False True 50178 /   False False   False    

4)保存.csproj文件并重新加载项目。

5)将Web.Config文件添加到项目中,并添加以下几乎没有代码。 您可以根据您的要求稍后添加更多内容。

                      

6)添加Global.asax文件。 下面是一个示例文件。

  public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { RegisterRoutes(); } private void RegisterRoutes() { // Edit the base address of Service1 by replacing the "Service1" string below RouteTable.Routes.Add(new ServiceRoute("YourService", new WebServiceHostFactory(), typeof(YourServiceClass))); } } 

7)最后在项目的属性中,在Build选项卡下,如果输出路径设置为bin\Debug ,则将其修改为bin\

WCF不是点网。 要创建WCF应用程序,您必须做四件事

  1. 定义服务合同
  2. 在服务器端实现合同
  3. 托管您实施的服务
  4. 创建也可以使用服务合同的客户端

看看这个教程

这是服务及其主机的完整示例

 using System.ServiceModel; using System.ServiceModel.Description; using System.Runtime.Serialization; using System; [ServiceContract] public interface AddStuff { [OperationContract] int Add(int X,int Y); } public class opAddStuff : AddStuff { public int Add(int X, int Y) { return X + Y; } } public class Pgm { static void Main(string[] args) { string httpAddr = "http://127.0.0.1:6001/AddStuff"; string netAddr= "net.tcp://127.0.0.1:5001/AddStuff"; System.ServiceModel.ServiceHost SH = new ServiceHost(typeof(opAddStuff),new Uri(httpAddr)); BasicHttpBinding B = new BasicHttpBinding(); NetTcpBinding NB = new NetTcpBinding(); SH.AddServiceEndpoint(typeof(AddStuff), B, httpAddr); SH.AddServiceEndpoint(typeof(AddStuff), NB, netAddr); System.ServiceModel.Description.ServiceMetadataBehavior smb = SH.Description.Behaviors.Find(); // If not, add one if (smb == null) smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; SH.Description.Behaviors.Add(smb); SH.AddServiceEndpoint( ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); SH.Open(); Console.WriteLine("Service at your service"); string crap = Console.ReadLine(); } } 

您还必须运行此命令

netsh http add urlacl url = http:// +:6001 / AddStuff user = DOMAIN \ USER

其中一些来自这里