2009-11-18 67 views
5

我創建了一個項目作爲一個類庫。現在我需要把它變成一個WCF。我可以創建一個WCF項目,但我想避免與TFS大驚小怪。我已經完成了App.config並將/client:"wcfTestClient.exe「行添加到命令行參數中。但是似乎還有其他東西在啓動主機時丟失了。我需要採取什麼步驟將類庫轉換爲WCF?

回答

0

WCF不是網點。要創建一個WCF應用程序,你必須做四件事情

  1. 定義合同
  2. 服務實現在服務器端
  3. 主機您實現的服務
  4. 合同創建也可以使用該服務的客戶端合同

看看這個tutorial

氏s是服務的一個完整的例子,它的主機

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<ServiceMetadataBehavior>(); 
     // 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添加urlacl URL = http://+:6001/AddStuff用戶=域\用戶

一些這個來自here

+0

我已經創建了服務合同。實施它。我在主持人處遇到問題。當你創建一個WCF應用程序時,它會爲你做。我想修改一個類庫應用程序來做到這一點。 – 2009-11-18 22:10:50

+0

您仍在爲服務啓動一個exe文件並實例化該服務。這就是我的問題,我需要Visual Studio自己弄清楚它在WCF服務庫中的作用。 – 2009-11-19 14:58:28

12

我發現下面的做法與您正在嘗試實現的相反,即將服務庫更改爲控制檯應用程序。

csproj文件中的某些設置不能從VS中的設置屏幕進行編輯,以將類庫轉換爲WCF服務庫,您需要將以下內容添加到項目文件中

將以下內容添加到第一個PropertyGroup [這是一個C#WCF項目的GUID]

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

在這裏看到進一步的信息,ProjectTypeGuids

您可能還需要添加以下行正下方:

<StartArguments>/client:"WcfTestClient.exe"</StartArguments> 

但最終它是您需要手動插入的PropertyTypeGuids讓VS將項目識別爲WCF服務庫項目。

1

這是我不得不做的將我的類庫轉換爲WCF REST應用程序。

1)修改.csproj文件,並將以下兩行添加到.csproj文件中的第一個PropertyGroup元素。

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

2)下面的行添加到下面<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />導入Microsoft.WebApplication.targets文件

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" /> 

3)</Project>標記之前將以下代碼添加到該文件的末尾。

<ProjectExtensions> 
<VisualStudio> 
    <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}"> 
    <WebProjectProperties> 
     <UseIIS>False</UseIIS> 
     <AutoAssignPort>True</AutoAssignPort> 
     <DevelopmentServerPort>50178</DevelopmentServerPort> 
     <DevelopmentServerVPath>/</DevelopmentServerVPath> 
     <IISUrl> 
     </IISUrl> 
     <NTLMAuthentication>False</NTLMAuthentication> 
     <UseCustomServer>False</UseCustomServer> 
     <CustomServerUrl> 
     </CustomServerUrl> 
     <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile> 
    </WebProjectProperties> 
    </FlavorProperties> 
</VisualStudio> 

4)保存的.csproj文件和刷新該項目。

5)將Web.Config文件添加到項目中,並添加下面的最小代碼。您可以根據您的要求添加更多內容。

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
    </system.webServer> 

    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 

</configuration> 

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\

相關問題