如何动态调用 net.tcp Web 服务 [英] How to dynamically call a net.tcp Web Service

查看:23
本文介绍了如何动态调用 net.tcp Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 C# 桌面应用程序调用 WCF,但出现错误.这是我的代码:

I am trying to call a WCF from my C# desktop app but i get an error. This is my code:

//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();

但收到此错误对象引用未设置到对象的实例."

But get this error 'Object reference not set to an instance of an object.'

关于 .Open() 方法.

on the .Open() method.

此外,定义了WindowsFormsApplication2.ws.ISync2Channel",因为我已经添加了对服务的引用.如果我没有我会有什么改用?

Also, the 'WindowsFormsApplication2.ws.ISync2Channel' is defined because I had already added a reference to the Service. If I had not what would I have used instead?

///********************新设置:

///******************new settings:

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>

服务器网络配置



-->



-->

我得到的错误在这一行:

The error I get is on this line:

wcf.UploadMotionDynamic2(new byte[1]{1},0,0);

错误是:你调用的对象是空的.堆栈是:

The error is: Object reference not set to an instance of an object. the stack is:

服务器堆栈跟踪:在 System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri, TimeSpan timeout)在 System.ServiceModel.Channels.BufferedConnectionInitiator.Connect(Uri uri, TimeSpan timeout)在 System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)在 System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)在 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)在 System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)在 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)在 System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)在 System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan 超时,CallOnceManager 级联)在 System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

Server stack trace: at System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri, TimeSpan timeout) at System.ServiceModel.Channels.BufferedConnectionInitiator.Connect(Uri uri, TimeSpan timeout) at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout) at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade) at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 类型)在 WindowsFormsApplication2.ws.ISync2.UploadMotionDynamic2(Byte[] jpegData, Int32 status, Int32 framePart)在 WindowsFormsApplication2.ws.Sync2Client.UploadMotionDynamic2(Byte[] jpegData, Int32 status, Int32 framePart) in 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

Exception rethrown at [0]: 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) in m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Service References\ws\Reference.cs:line 81 at WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) in m:\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs:line 581

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

This all works when I use http binding though...

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

The port 780 is open on my server. the Net.Tcp.Listener Adaptor is running Enabled Protocols in IIS has net.tcp

仍然没有快乐

推荐答案

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

It looks like your using the wrong constructor for your requirements. When using the ChannelFactory(String, EndpointAddress) constructor, the first parameter stands for the name of your endpoint configuration (in your config file).

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

Using the ChannelFactory(Binding, EndpointAddress) constructor will fulfill your requirement of passing the configuration programmatically.

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

Use this constructor when you want to pass the binding and address information programmatically rather than using an application configuration file.

这篇关于如何动态调用 net.tcp Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆