【问题标题】:What steps do I need to take to convert from a class library to a WCF?从类库转换为 WCF 需要采取哪些步骤?
【发布时间】:2010-12-18 01:47:54
【问题描述】:

我创建了一个项目作为类库。现在我需要把它变成一个WCF。我可以创建一个 WCF 项目,但我想避免对 TFS 大惊小怪。我已经完成了 App.config 并将 /client:"wcfTestClient.exe" 行添加到命令行参数中。但是启动托管似乎还缺少其他东西。

【问题讨论】:

    标签: c# wcf hosting app-config


    【解决方案1】:

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

    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 服务库项目。

    【讨论】:

      【解决方案2】:

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

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

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

      2) 将以下行添加到&lt;Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /&gt; 下面以导入Microsoft.WebApplication.targets 文件

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

      3) 将以下代码添加到文件末尾&lt;/Project&gt; 标记之前。

      <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\

      【讨论】:

        猜你喜欢
        • 2010-11-05
        • 2012-06-30
        • 1970-01-01
        • 2016-11-28
        • 2017-09-04
        • 2021-03-29
        • 2011-01-18
        • 2010-12-14
        • 1970-01-01
        相关资源
        最近更新 更多