What steps should I take to convert from a class library to WCF?

I created the project as a class library. Now I need to do this in WCF. I can create a WCF project, but I would like to avoid all these problems with TFS. I made App.config and added the line / client: "wcfTestClient.exe" to the command line arguments. But, it seems, something else is missing from him, starting hosting.

+5
source share
3 answers

I found the following, the opposite of what you are trying to achieve, i.e. Change the service library to a console application.

csproj VS WCF, :

PropertyGroup [ # WCF]

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

. ProjectTypeGuids

:

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

PropertyTypeGuids, , VS WCF.

+12

, , WCF REST.

1) .csproj PropertyGroup .csproj.

<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) , , bin\Debug, bin\.

+1

WCF . WCF,

  • ,

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 add urlacl url = http://+:6001/AddStuff user = DOMAIN\USER

0

All Articles