【问题标题】:How to dynamically call a net.tcp Web Service如何动态调用 net.tcp Web 服务
【发布时间】:2014-07-30 09:39:12
【问题描述】:

我正在尝试从我的 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,TimeSpan 超时) 在 System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan 超时) 在 System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(时间跨度超时) 在 System.ServiceModel.Channels.CommunicationObject.Open(时间跨度超时) 在 System.ServiceModel.Channels.ServiceChannel.OnOpen(时间跨度超时) 在 System.ServiceModel.Channels.CommunicationObject.Open(时间跨度超时) 在 System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel 通道,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan 超时,CallOnceManager 级联) 在 System.ServiceModel.Channels.ServiceChannel.EnsureOpened(时间跨度超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage 方法调用,ProxyOperationRuntime 操作) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

在 [0] 处重新抛出异常: 在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) 在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 类型) 在 WindowsFormsApplication2.ws.ISync2.UploadMotionDynamic2(字节 [] jpegData,Int32 状态,Int32 框架部分) 在 WindowsFormsApplication2.ws.Sync2Client.UploadMotionDynamic2(Byte[] jpegData, Int32 status, Int32 framePart) 在 m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Service References\ws\Reference.cs:line 81 在 WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) in m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs:line 581

当我使用 http 绑定时,这一切都有效......

我的服务器上的端口 780 已打开。 Net.Tcp.Listener 适配器正在运行 IIS 中启用的协议有 net.tcp

还是不开心

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    看起来您使用了错误的构造函数来满足您的要求。 使用ChannelFactory(String, EndpointAddress) 构造函数时,第一个参数代表您的端点配置的名称(在您的配置文件中)。

    使用ChannelFactory(Binding, EndpointAddress) 构造函数将满足您以编程方式传递配置的要求。

    当您想以编程方式而不是使用应用程序配置文件传递绑定和地址信息时,请使用此构造函数。

    【讨论】:

    • 您好,感谢您帮助我。我已根据您的建议更新了我的问题,但我仍然收到错误...
    • 您收到的异常是您在ISync2Channel接口中定义的Open()方法抛出的,与您的ChannelFactory无关。
    • 您好,再次感谢您帮助我。归根结底,我仍然无法动态打开/创建 Web 服务的客户端代理。
    • 如果我直接在我的服务器上调用此服务,它就可以工作,所以这与我的客户端代码有关,这就是我要在这里解决的问题 :)
    • 当您尝试触发 UploadMotionDynamic2() 方法时,不调用 Open() 方法(它甚至做了什么?),会发生什么?
    【解决方案2】:

    您可以使用 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

    【讨论】:

    • 请详细说明您的问题
    • 您好,感谢您的评论。我正在使用 net.tcp。不过谢谢
    猜你喜欢
    • 2010-11-18
    • 2011-01-30
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2012-07-18
    • 1970-01-01
    相关资源
    最近更新 更多