我试图从我的C#桌面应用程序调用一个WCF,但我得到了一个错误。这是我的密码:
//Client Code
System.ServiceModel.EndpointAddress addressSync = new System.ServiceModel.EndpointAddress("net.tcp://an ip address/Sync2.svc");
Shared.FactorySync = new System.ServiceModel.ChannelFactory<LiteEdition.wsSyncFastest.ISync2Channel>("NetTcpBinding_ISync2", addressSync);
Shared.UpLoadSync = Shared.FactorySync.CreateChannel();问题。如果我动态地创建一个对WCF的客户端调用,我可以假设我不需要在app.config文件中包含任何有关绑定?的内容。
//error message on this line:
Shared.UpLoadSync.UploadMotionDynamic2(new byte[1]{0}, 0, 0);
//IS:
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9349963'。
//My Service:
public void UploadMotionDynamic2(byte[] jpegData, int status, int framePart)
{
DAL dal = new DAL();
try
{
if (jpegData != null)
{
LiveView2.SetNewFrame(status, framePart, jpegData);
}
}
catch (Exception ex)
{
email.SendError("Sync.UploadMotionDynamic:" + ex.ToString(), "");
}
}
//In my Web.Config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NetTCPBehaviour">
<serviceTimeouts transactionTimeout="0.00:00:30" />
<serviceDebug includeExceptionDetailInFaults="false" />
<dataContractSerializer maxItemsInObjectGraph="65536" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="testme" behaviorConfiguration="NetTCPBehaviour">
<endpoint address="Sync2.svc" binding="netTcpBinding" contract="ISync" name="wsMotionUploader" bindingConfiguration="NetTCPBindingEndPoint">
<!--<security mode="None"></security>-->
</endpoint>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="NetTCPBindingEndPoint" receiveTimeout="00:15:00" sendTimeout="00:15:00" transferMode="Streamed" closeTimeout="00:02:00" openTimeout="00:02:00"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxArrayLength="32768" />
<security mode="None">
<transport clientCredentialType="None" protectionLevel="None" />
<message clientCredentialType="None" />
</security>
</binding>
</netTcpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel> 经修订的守则:
System.ServiceModel.NetTcpBinding binding = new System.ServiceModel.NetTcpBinding();
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("http:ip/Sync2");
System.ServiceModel.ChannelFactory<System.ServiceModel.Channels.IRequestChannel> factory =
new System.ServiceModel.ChannelFactory<System.ServiceModel.Channels.IRequestChannel>(binding, address);
var channel = factory.CreateChannel();
channel.Open();更新如下:
System.ServiceModel.NetTcpBinding binding = new System.ServiceModel.NetTcpBinding();
System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress("net.tcp://ip/Sync2");
System.ServiceModel.ChannelFactory<WindowsFormsApplication2.ws.ISync2Channel> factory =
new System.ServiceModel.ChannelFactory<WindowsFormsApplication2.ws.ISync2Channel>(binding, address);
factory.Endpoint.Contract.SessionMode = System.ServiceModel.SessionMode.Allowed;
WindowsFormsApplication2.ws.ISync2Channel channel = factory.CreateChannel();
channel.Open();但是,获取此错误“对象引用未设置为对象的实例”。
在.Open()方法上。
此外,定义'WindowsFormsApplication2.ws.ISync2Channel‘是因为我已经添加了对服务的引用。如果我没有,我会用什么代替?
//*新设置:
Client code:
ws.Sync2Client wcf = new ws.Sync2Client("NetTcpBinding_ISync2");
wcf.UploadMotionDynamic2(new byte[1]{1},0,0);
Client App.Config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISync2" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_ISync2" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://ip/Sync2.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISync2"
contract="ws.ISync2" name="BasicHttpBinding_ISync2" />
<endpoint address="net.tcp://ip/Sync2.svc" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_ISync2" contract="ws.ISync2"
name="NetTcpBinding_ISync2">
<identity>
<servicePrincipalName value="host/ip" />
</identity>
</endpoint>
</client>
</system.serviceModel>服务器Web.Config
--> 我得到的错误在这条线上:
wcf.UploadMotionDynamic2(new byte[1]{1},0,0);错误是:对象引用未设置为对象的实例。堆栈是:
服务器堆栈跟踪:在System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri,TimeSpan超时值)在System.ServiceModel.Channels.BufferedConnectionInitiator.Connect(Uri uri,( System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan超时)在System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan超时( System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时值) System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan超时值( System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时值) System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel通道) TimeSpan超时( System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan超时),(在System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan超时值))( System.ServiceModel.Channels.ServiceChannel.Call(String动作,布尔单向,ProxyOperationRuntime操作,Object[] ins,Object[],TimeSpan超时值)在System.ServiceModel.Channels.ServiceChannel.Call(String动作,布尔单向,ProxyOperationRuntime操作,Object[] ins,Object[] outs)在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息))
异常重抛于: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 type) at WindowsFormsApplication2.ws.ISync2.UploadMotionDynamic2(Byte[] jpegData,Int32 status,Int32 framePart) at WindowsFormsApplication2.ws.Sync2Client.UploadMotionDynamic2(Byte[] jpegData,Int32 status,Int32 framePart)在m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Service 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Service 81 at发件人处,( m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs:line 581中的EventArgs e)
不过,当我使用http绑定时,所有这些都能工作.
端口780在我的服务器上是打开的。IIS中的Net.Tcp.Listener适配器正在运行已启用的协议net.tcp
仍然没有joy
发布于 2014-07-30 10:13:18
看起来您对需求使用了错误的构造函数。当使用ChannelFactory(String,EndpointAddress)构造函数时,第一个参数表示端点配置的名称(在配置文件中)。
使用ChannelFactory(绑定,EndpointAddress)构造函数将满足以编程方式传递配置的要求。
如果希望以编程方式传递绑定和地址信息,而不是使用应用程序配置文件,请使用此构造函数。
发布于 2014-07-30 11:36:24
您可以使用ChannelFactory类来调用wcf服务,在这种情况下,您不需要在配置文件中有端点和绑定。您可以在c#中创建的所有内容(端点、行为)如下所示
var channelFactory = new ChannelFactory<IService>(new BasicHttpBinding(), "http://myurl:8080/ServiceFirst");
IService client = channelFactory.CreateChannel();查看这个链接http://msdn.microsoft.com/en-us/library/ms734681(v=vs.110).aspx
https://stackoverflow.com/questions/25033483
复制相似问题