例如,我在IIS中托管了两个服务.
[ServiceContract]
public interface IDeviceService
{
[OperationContract]
DeviceCollection GetAllDevices(Customer customer);
}
[ServiceContract]
public interface IUserService
{
[OperationContract]
User Authenticate(string username, string password);
}
Run Code Online (Sandbox Code Playgroud)
从UserService中的Authenticate操作返回的User对象和从DeviceService中的GetAllDevices操作返回的DeviceCollection都具有Customer的子对象定义.Customer是一个业务对象与User和Device对象在同一个程序集中.
我的问题出在客户端 - 当我调用设备操作时
userProxy.GetAllDevices(user.Customer);
Run Code Online (Sandbox Code Playgroud)
编译器抱怨以下消息:
参数1 - 无法从UserService.Customer转换为DeviceService.Customer
我可以很好地连接到两个服务,这是Customer的对象定义就是问题.我真的不想把操作放在他们看似在自己的服务中自然生活的相同服务中.我想我要问的是,其他程序员如何处理这样的问题呢?
干杯,斯图尔特
我在网上找到了我的问题的混合答案.详细说明问题:
我发现很多博主和论坛海报相互矛盾.任何人都可以指出任何明确的消息来源或证据来一劳永逸地回答这个问题吗?
如何确保WCF ChannelFactory在xml配置中使用绑定设置(忽略MaxArrayLength)
嗨,我是Wcf的新手,并且写了我的第一个Wcf服务和客户端.我不想使用工具来生成配置; 我宁愿自己编写配置.我试图解决的问题是客户端通过netTcp与服务进行通信.该服务可能会返回非常大的有效负载(比默认的readerQuotas.maxArrayLength更好).当字节流有效载荷相对较低时(即小于它认为约为16K的默认值),我最初开发的组件工作正常.我可以通过创建绑定并将MaxArrayLength设置为足够大的值来以编程方式解决此问题.但是,我需要能够在xml配置中执行等效操作.
我的app.config(客户端)是:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:9000/WcfDataService/RemoteDataRequesterService/"
binding="netTcpBinding" bindingConfiguration="unsecureNetTcpBinding"
contract="WcfDataServiceLib.IRemoteDataRequester"
name="DataRequesterEndpoint" />
</client>
<bindings>
<netTcpBinding>
<binding name="unsecureNetTcpBinding" maxReceivedMessageSize="2147483647">
<readerQuotas maxArrayLength="1000000" />
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)
创建客户端代理的代码如下:
private void Init()
{
var address = new EndpointAddress(@"net.tcp://localhost:9000/WcfDataService/RemoteDataRequesterService/");
const string endpointName = "DataRequesterEndpoint";
ChannelFactory<IRemoteDataRequester> factory = new ChannelFactory<IRemoteDataRequester>(
endpointName);
IRemoteDataRequester proxy = factory.CreateChannel(address);
// call business methods on proxy ...
}
Run Code Online (Sandbox Code Playgroud)
请注意,代码通过变量"endpointName"链接到配置.
服务端配置(我不认为这是相关的,但包括完整性):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfDataServiceLib.RemoteDataRequesterService" …Run Code Online (Sandbox Code Playgroud) 只要操作完成,WCF服务中的OneWay操作是否会执行?
根据我的实验,我认为没有超时.我能够进行半小时的手术.(之后我关门了)
在WCF有经验的人可以回答这个吗?如果超时,我可以在哪里指定它
我有一个调用WCF Web服务的WCF客户端(控制台应用程序),我正在尝试从我的控制台应用程序中获取原始XML响应.
有没有人有关于如何做到这一点的想法或代码片段?
一位客户在我的WCF客户端连接到我们的服务器服务时报告了此错误.
"消息:尝试对无法访问的主机执行套接字操作类型:System.Net.Sockets.SocketException"
从这个链接http://msdn.microsoft.com/en-us/library/ms740668%28v=vs.85%29.aspx我看到:
WSAEHOSTUNREACH 10065无东道主路线.尝试对无法访问的主机执行套接字操作.见WSAENETUNREACH.
WSAENETUNREACH 10051网络无法访问.尝试对无法访问的网络执行套接字操作.这通常意味着本地软件不知道到达远程主机的路由.
这是客户网络上的网络错误吗?这是我们可以建议客户做的事情吗?
WCF客户端之前能够连接到服务器服务,并且在重新启动计算机之后似乎问题消失了(或者在此期间可能修复了网络问题).
谢谢
阿德里安娜
我正在尝试使用安全性配置WCF服务.我已经生成了2个存储在LocalComputer\Personal Certificates中的证书(用于服务器端和客户端).我的配置是:
服务器:
<netTcpBinding>
<binding name="defaultBinding">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
</netTcpBinding>
<service name="..." behaviorConfiguration="serviceBehavior">
<endpoint address="..." binding="netTcpBinding" bindingConfiguration="defaultBinding" contract="...">
<identity>
<dns value="ClientSide"/>
</identity>
</endpoint>
</service>
<behavior name="serviceBehavior">
<serviceCredentials>
<serviceCertificate storeLocation="LocalMachine" storeName="My" findValue="ServerSide" x509FindType="FindBySubjectName"/>
<clientCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</clientCertificate>
</serviceCredentials>
<behavior>
Run Code Online (Sandbox Code Playgroud)
客户:
<netTcpBinding>
<binding name="defaultBinding">
<security mode="Transport">
<transport clientCredentialType="Certificate"/>
</security>
</binding>
</netTcpBinding>
<endpoint name="..." binding="netTcpBinding" bindingConfiguration="defaultBinding" contract="..."
behaviorConfiguration="endpointBehavior">
<identity>
<dns value="ServerSide"/>
</identity>
</endpoint>
<behavior name="endpointBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None" revocationMode="NoCheck"/>
</serviceCertificate>
<clientCertificate storeLocation="LocalMachine" storeName="My" findValue="ClientSide" x509FindType="FindBySubjectName"/>
</clientCredentials>
<behavior>
Run Code Online (Sandbox Code Playgroud)
我收到异常: …
我正在构建的应用程序公开了几个WCF服务(A,B).在内部,它消耗了我们内部网络(X,Y)上运行的其他几个WCF服务.
使用WCF消息记录,我希望仅记录我们的服务A,B和调用它们的外部客户端之间的流量.
WCF不应记录我的服务(A,B)和后端服务(X,Y)之间的数据.
通过system.serviceModel/diagnostics/messageLogging/filters过滤部分成功:
<filters>
<add nodeQuota="10" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
/s:Envelope/s:Header/*[contains(text(),"MyServiceA")]
</add>
<add nodeQuota="10" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">
/s:Envelope/s:Header/a:Action[contains(text(),"MyServiceA")]
</add>
</filters>
Run Code Online (Sandbox Code Playgroud)
但是,这无法捕获来自我们服务的响应,因为SOAP 响应不包含要过滤的文本.
WCF MessageLogTraceRecord 确实包含SOAP Action,但我似乎无法构造一个过滤器来访问它:
<MessageLogTraceRecord>
<Addressing xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace>
<Action>http://opia.api.translink.com.au/ApiLocationService/2012/04/IApiLocationService/ResolveInputServiceFaultFault</Action>
</Addressing>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
...
Run Code Online (Sandbox Code Playgroud)
启用WCF消息日志记录和端到端跟踪,并将所有选项设置为true.已启用ActivityTracing和警告级别日志记录.
我添加了对具有两个端点的WCF服务的引用.在添加服务时,以下内容将添加到Config文件中:
<client>
<endpoint name="ABCServiceV1" address="http://staging.ABCwebservices.com/ABC/Service.svc"
binding="basicHttpBinding" bindingConfiguration="ABCServiceV1"
contract="ABCService.IService" />
<endpoint name="ABCServiceV2" address="http://staging.ABCwebservices.com/ABC/Service.svc/20"
binding="basicHttpBinding" bindingConfiguration="ABCServiceV2"
contract="ABCService.IService1" />
</client>
Run Code Online (Sandbox Code Playgroud)
创建客户端的代码如下:
ABCService.ServiceClient ABCClient = new ServiceClient("ABCServiceV2");
Run Code Online (Sandbox Code Playgroud)
但是,我收到运行时错误 - "找不到名称为'ABCServiceV2'的端点元素,并在ServiceModel客户端配置部分中收缩'ABCService.IService'.这可能是因为找不到您的应用程序的配置文件,或者因为没有匹配此名称的端点元素可以在客户端元素中找到."
如果我使用ABCService.ServiceClient ABCClient = new ServiceClient("ABCServiceV1");然后一切正常.但是当使用ABCServiceV2时,它正在尝试寻找Contract - ABCService.IService - 它应该在什么时候寻找 - ABCService.IService1.
我如何让它寻找正确的合同?
我有一个非Wcf服务器,我从WCF客户端调用,我需要访问已注册的soap故障,如果服务器抛出它(它包含我需要用户的反馈).我使用了如何从WCF客户端访问SOAP 1.1故障详细信息中的示例(没有错误契约),但是给定了一个痛苦我已经在wsdl中定义了故障契约,至少每个SOAP规范,并且错误包含错误代码和错误字符串.
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="http://www.cisco.com/BTS10200/i01" xmlns="http://www.w3.org/2001/XMLSchema">
...
<complexType name="BtsSoapException">
<sequence>
<element name="error_code" type="xsd:int"/>
<element name="error_string" nillable="true" type="xsd:string"/>
</sequence>
</complexType>
<element name="fault" type="impl:BtsSoapException"/>
...
<wsdl:message name="BtsSoapException">
<wsdl:part element="impl:fault" name="fault"/>
</wsdl:message>
...
<wsdl:portType name="Bts10200Operations">
<wsdl:operation name="login">
<wsdl:input message="impl:loginRequest" name="loginRequest"/>
<wsdl:output message="impl:loginResponse" name="loginResponse"/>
<wsdl:fault message="impl:BtsSoapException" name="BtsSoapException"/>
</wsdl:operation>
...
Run Code Online (Sandbox Code Playgroud)
服务导入可以正确识别所有这些并生成正确的代码构造:
[System.Runtime.Serialization.DataContractAttribute(Name="BtsSoapException", Namespace="http://www.cisco.com/BTS10200/i01")]
[System.SerializableAttribute()]
public partial class BtsSoapException : object ...
....
[System.ServiceModel.ServiceContractAttribute(Namespace="http://www.cisco.com/BTS10200/i01", ConfigurationName="CiscoBTSService.Bts10200Operations")]
public interface Bts10200Operations {
[System.ServiceModel.OperationContractAttribute(Action="", ReplyAction="*")]
[System.ServiceModel.FaultContractAttribute(typeof(TestCiscoBTS.CiscoBTSService.BtsSoapException), Action="", Name="fault")]
TestCiscoBTS.CiscoBTSService.loginResponse login(TestCiscoBTS.CiscoBTSService.loginRequest request);
...
Run Code Online (Sandbox Code Playgroud)
当我login()使用无效帐户调用时,我会根据wsdl获得正确的响应: …
wcf-client ×10
wcf ×9
c# ×3
.net ×2
binding ×1
certificate ×1
config ×1
networking ×1
silverlight ×1
soap ×1
wcf-binding ×1
wcf-proxy ×1
wcf-security ×1