在没有 WCF 服务库的情况下从客户端使用 WCF 服务 project/assembly 参考
Consume WCF service from client without WCF Service Library project/assembly reference
以下问题可能重复,但没有足够的信息来回答我的问题。
Is possible to access WCF Service without adding Service Reference?
我按照本指南在 Visual Studio 中使用 WCF 服务库项目设置了 TCP 服务。
https://msdn.microsoft.com/en-us/library/ff649818.aspx
如果您从客户端(Windows Forms 项目)添加对 WCF 服务库项目的引用,效果会很好。并在项目程序集中添加一条using语句,使用这段代码。
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.
所以我尝试使用此代码。我添加了对 System.ServiceModel 的引用(和一个 using 指令)来解决大部分错误。但现在它说 "The type or namespace name 'ServiceContract' could not be found (are you missing a using directive or an assembly reference?)"。我是否需要在我的服务中修改 App.Config,或向调用下面代码的项目添加任何其他代码(关于 'ServiceContract')?
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);
注:我将默认的Service1.cs和默认的接口IService1.cs重命名为Service.cs和IService.cs。没有“1”作为后缀,基于说明。我还在代码中将 WcfServiceLibrary1 重命名为 TcpService。此外,在 .NET 4.5 中,我根本不需要这行代码就可以运行。这个.Close() 出错了。
myService.Close();
我在 Visual Studio 中添加了一个服务引用,方法是右键单击“项目”>“添加”>“服务引用”。但是我在那里遇到错误。
第二个对话框中的错误:
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.
指南缺少一个步骤。除了 httpGetEnabled
属性之外,我还设置了 httpsGetEnabled
到 "false",而且它起作用了!之后,我在添加服务引用时不再收到错误。然后我将 'ServiceContract' 更改为 'MyClientProject.ServiceReference1.IService' 以引用新的服务参考。
<?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>
以下问题可能重复,但没有足够的信息来回答我的问题。
Is possible to access WCF Service without adding Service Reference?
我按照本指南在 Visual Studio 中使用 WCF 服务库项目设置了 TCP 服务。
https://msdn.microsoft.com/en-us/library/ff649818.aspx
如果您从客户端(Windows Forms 项目)添加对 WCF 服务库项目的引用,效果会很好。并在项目程序集中添加一条using语句,使用这段代码。
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.
所以我尝试使用此代码。我添加了对 System.ServiceModel 的引用(和一个 using 指令)来解决大部分错误。但现在它说 "The type or namespace name 'ServiceContract' could not be found (are you missing a using directive or an assembly reference?)"。我是否需要在我的服务中修改 App.Config,或向调用下面代码的项目添加任何其他代码(关于 'ServiceContract')?
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);
注:我将默认的Service1.cs和默认的接口IService1.cs重命名为Service.cs和IService.cs。没有“1”作为后缀,基于说明。我还在代码中将 WcfServiceLibrary1 重命名为 TcpService。此外,在 .NET 4.5 中,我根本不需要这行代码就可以运行。这个.Close() 出错了。
myService.Close();
我在 Visual Studio 中添加了一个服务引用,方法是右键单击“项目”>“添加”>“服务引用”。但是我在那里遇到错误。
第二个对话框中的错误:
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.
指南缺少一个步骤。除了 httpGetEnabled
属性之外,我还设置了 httpsGetEnabled
到 "false",而且它起作用了!之后,我在添加服务引用时不再收到错误。然后我将 'ServiceContract' 更改为 'MyClientProject.ServiceReference1.IService' 以引用新的服务参考。
<?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>