如何动态调用net.tcp Web服务

时间:2014-07-30 09:39:13

标签: c# wcf

我试图从我的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();

但是得到这个错误'对象引用未设置为对象的实例。'

<。> on .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

                                                            
          
                                 - &GT;                                                                                                                                                                                                                                                                                      

我得到的错误就在这一行:

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

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

服务器堆栈跟踪:    在System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri,TimeSpan timeout)    在System.ServiceModel.Channels.BufferedConnectionInitiator.Connect(Uri uri,TimeSpan超时)    在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超时,CallOnceManager级联)    在System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan超时)    在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操作)    在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&amp; msgData,Int32 type)    在WindowsFormsApplication2.ws.ISync2.UploadMotionDynamic2(字节[] jpegData,Int32状态,Int32 framePart)    at WindowsFormsApplication2.ws.Sync2Client.UploadMotionDynamic2(Byte [] jpegData,Int32 status,Int32 framePart)在m:\ Visual Studio 2013 \ Projects \ WindowsFormsApplication2 \ WindowsFormsApplication2 \ Service References \ ws \ Reference.cs:第81行    at WindowsFormsApplication2.Form1.button1_Click(Object sender,EventArgs e),位于m:\ Visual Studio 2013 \ Projects \ WindowsFormsApplication2 \ WindowsFormsApplication2 \ Form1.cs:第581行

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

端口780在我的服务器上打开。 Net.Tcp.Listener适配器正在运行 IIS中的Enabled Protocols具有net.tcp

仍然没有快乐

2 个答案:

答案 0 :(得分:0)

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

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

  

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

答案 1 :(得分:0)

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