C# 客户端上的wcf服务器初始化方法
我在我的WCF服务器上有一个执行回调的方法,如下所示。包括回调在内,这一切都很好 但是,如何从服务器在客户机上启动该方法的执行呢。ie的代码在C部分按钮代码我想在下面 目前我得到的错误是:C# 客户端上的wcf服务器初始化方法,c#,.net,wcf,wcf-binding,C#,.net,Wcf,Wcf Binding,我在我的WCF服务器上有一个执行回调的方法,如下所示。包括回调在内,这一切都很好 但是,如何从服务器在客户机上启动该方法的执行呢。ie的代码在C部分按钮代码我想在下面 目前我得到的错误是: Object reference not set to an instance of an object. 可能我的架构都错了,WCF客户端也需要内置WCF服务器 WCF服务器上的C方法 在您的情况下,我建议您使用wsDualHttpBinding:一种安全且可互操作的绑定,它设计用于双工服务契约,允许服务
Object reference not set to an instance of an object.
可能我的架构都错了,WCF客户端也需要内置WCF服务器
WCF服务器上的C方法
在您的情况下,我建议您使用wsDualHttpBinding:一种安全且可互操作的绑定,它设计用于双工服务契约,允许服务和客户端发送和接收消息。
请参见示例:在您的情况下,我建议您使用wsDualHttpBinding:一种安全且可互操作的绑定,设计用于双工服务契约,允许服务和客户端发送和接收消息。
请参阅以下示例:调试代码并尝试了解哪个对象引发该异常。代码中的某个位置有1个对象未正确初始化。它位于RadButton中的行,单击IMyContractCallBack callback=OperationContext.Current.GetCallbackChannel;-我正在尝试从服务器启动客户端上的方法调试代码并检查OperationContext或OperationContext.Current是否为null。PS:如果您可以发布客户端应用程序和WCF服务的WCF配置,这将是一件好事。是OperationContext.Current为空-请记住,我试图从服务器在客户端上执行一个方法。关于我需要做什么有什么想法吗?这应该是一个配置问题…调试代码并尝试了解哪个对象引发了该异常。代码中的某个位置有1个对象未正确初始化。它位于RadButton中的行,单击IMyContractCallBack callback=OperationContext.Current.GetCallbackChannel;-我正在尝试从服务器启动客户端上的方法调试代码并检查OperationContext或OperationContext.Current是否为null。PS:如果您可以发布客户端应用程序和WCF服务的WCF配置,这将是一件好事。是OperationContext.Current为空-请记住,我试图从服务器在客户端上执行一个方法。关于我需要做什么有什么想法吗?这应该是一个配置问题…嗨-我使用的是duplex-在第一种方法中,客户机发送一个请求,然后运行这个方法,该方法向客户机回调。-在服务器上的第二个方法中-我想告诉客户机运行一些代码-哦,然后,您可以显示您的配置设置和数据合同吗?添加了配置和合同您是否与客户端的IWCFJobsLibrary和IMyContractCallBack以及服务器端的IWCFJobsLibrary和IMyContractCallBack签订了合同?IWCFJobsLibrary和IMyContractCallBack仅存在于服务器上-这是我的问题吗?嗨-我正在使用duplex--在第一种方法中,客户机发送一个请求,然后运行该方法,该方法将回调客户机。-在服务器上的第二个方法中-我想告诉客户机运行一些代码-哦,然后,您可以显示您的配置设置和数据合同吗?添加了配置和合同您是否在客户端与IWCFJobsLibrary和IMyContractCallBack以及服务器端与IWCFJobsLibrary和IMyContractCallBack签订了合同?IWCFJobsLibrary和IMyContractCallBack仅存在于服务器上?这是我的问题吗?
public void ChatToServer(string texttoServer) // send some text to the server
{
Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
try
{
IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>();
callback.ChatToClient("your message: " + texttoServer + " has been recieved");
Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
}
catch (Exception ex)
{
Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), 2);
}
}
private void radButtonSend_Click(object sender, EventArgs e)
{
try
{
Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
IMyContractCallBack callback = OperationContext.Current.GetCallbackChannel<IMyContractCallBack>();
callback.ChatToClient(radTextBox2Send.Text);
Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
}
catch (Exception ex)
{
Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), 2);
MessageBox.Show(ex.Message.ToString());
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Throttled">
<serviceMetadata httpGetEnabled="False" />
<serviceDebug includeExceptionDetailInFaults="False" />
<serviceThrottling maxConcurrentCalls="100000" maxConcurrentInstances="100000" maxConcurrentSessions="100000" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WCFService.WCFJobsLibrary" behaviorConfiguration="Throttled" >
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFService/WCFJobsLibrary/" />
</baseAddresses>
</host>
<endpoint name ="duplexendpoint"
address =""
binding ="wsDualHttpBinding"
contract ="WCFService.IWCFJobsLibrary"/>
<endpoint name ="MetaDataTcpEndpoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="duplexendpoint" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/WCFService/WCFJobsLibrary/"
binding="wsDualHttpBinding" bindingConfiguration="duplexendpoint"
contract="ServiceReference1.IWCFJobsLibrary" name="duplexendpoint">
</endpoint>
</client>
</system.serviceModel>
</configuration>
namespace WCFService
{
[ServiceContract(CallbackContract = typeof(IMyContractCallBack))]
public interface IWCFJobsLibrary
{
[OperationContract(IsOneWay = true)]
void ChatToServer(string texttoServer); // send some text to the server
[OperationContract(IsOneWay = true)]
void NormalFunction();
[OperationContract(IsOneWay = false)]
int ChatToServerWithResult(string texttoServer); // send some text to the server and send back success indication
}
public interface IMyContractCallBack
{
[OperationContract(IsOneWay = true)]
void CallBackFunction(string str);
[OperationContract(IsOneWay = true)]
void ChatToClient(string str);
}
}