Consume WCF service from client without WCF Service Library project/assembly reference

老子叫甜甜 提交于 2019-12-23 03:34:26

问题


The following question may be a duplicate, but there wasn't enough information to answer my question from it.

Is possible to access WCF Service without adding Service Reference?

I've setup a TCP service, using a WCF Service Library project in Visual Studio by following this guide.

https://msdn.microsoft.com/en-us/library/ff649818.aspx

This worked wonderful if you add a reference to the WCF Service Library project from your client (Windows Forms project). And add a using statement to the project assembly and use this code.

        TcpService.Service myService = new TcpService.Service();
        myService.GetData(123);

However, I don't want to add a reference to the TcpService assembly in the project calling the method "GetData". Per the very first question's answer above.

So I tried using this code. I added a reference to System.ServiceModel (and a using directive) to resolve most of the errors. But now it says "The type or namespace name 'ServiceContract' could not be found (are you missing a using directive or an assembly reference?)". Do I need to modify the App.Config in my service, or add any other code (with regard to 'ServiceContract') to the project calling this code below?

        BasicHttpBinding binding = new BasicHttpBinding();
        EndpointAddress address = new EndpointAddress("net.tcp://localhost:8523/Service");
        ChannelFactory<ServiceContract> factory = new ChannelFactory<ServiceContract>(binding, address);
        ServiceContract channel = factory.CreateChannel();
        channel.GetData(123);

Note: I renamed the default Service1.cs and the default interface IService1.cs to Service.cs and IService.cs. Without the "1" as the suffix, based on the instructions. I also renamed WcfServiceLibrary1 to TcpService in my code. Also, in .NET 4.5, I didn't need this line of code at all for it to work. This .Close() errored out.

myService.Close();

I added a Service Reference in Visual Studio, by right clicking the Project > Add > Service Reference. But I get an error there.

Error in 2nd dialog:

The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8523/Service'. Could not connect to net.tcp://localhost:8523/Service. The connection attempt lasted for a time span of 00:00:01.0011001. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8523. No connection could be made because the target machine actively refused it 127.0.0.1:8523 If the service is defined in the current solution, try building the solution and adding the service reference again.


回答1:


The guide was missing a step. I set the httpsGetEnabled to "false" in addition to the httpGetEnabled attribute, and it worked! After that, I was no longer getting the error when adding a Service Reference. And then I changed the 'ServiceContract' to 'MyClientProject.ServiceReference1.IService' to be refer to the new Service Reference.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

...

    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>


来源:https://stackoverflow.com/questions/35071525/consume-wcf-service-from-client-without-wcf-service-library-project-assembly-ref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!