我的解决方案中有3个项目,基本上添加了两个数字并将值返回给客户端。我希望通过WCF中的这个简单项目完成从服务到客户端的回调。
- C#console app,作为客户端,通过提供要添加的2个值来调用WCF服务库。
- 作为服务的WCF服务库调用C#类库来进行添加。
- C#类库,由WCF服务库调用以添加这两个值,它执行添加两个值的工作并将值返回到WCF服务库,WCF服务库将值返回给C#控制台应用程序。
醇>WCF服务接口和实现接口的WCF服务类的配置也是这样的
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession) ServiceContract[(SessionMode = SessionMode.Required.)]
当C#console app的客户端调用WCF服务并且WCF服务执行添加并返回控制台应用程序时,我的项目工作正常。
如果客户端调用服务并且服务调用C#类库进行计算添加,则会给出“mscorlib.dll中发生的System.ServiceModel.CommunicationObjectFaultedException”。
WCF服务调用C#类库的方法是在方法中创建一个C#类库的实例,该实例由控制台应用程序调用并使用该实例调用该库的方法。
我想我需要以某种方式连接会话。如果我错了,请纠正我,因此它不会使通信对象成为故障状态。但我不知道该怎么做。其他任何想法也非常感谢。提前谢谢大家。
Client.cs
using System;
using System.ServiceModel;
namespace Microsoft.Samples.DualHttp
{
// The service contract is defined in generatedClient.cs, generated from the service by the svcutil tool.
// Define class which implements callback interface of duplex contract
public class CallbackHandler : ICalculatorDuplexCallback
{
public void Result(double result)
{
Console.WriteLine("Result({0})", result);
}
public void Equation(string eqn)
{
Console.WriteLine("Equation({0})", eqn);
}
}
class Client
{
static void Main()
{
// Construct InstanceContext to handle messages on callback interface
InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
// Create a client with given client endpoint configuration
CalculatorDuplexClient client = new CalculatorDuplexClient(instanceContext);
Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
Console.WriteLine();
// Call the AddTo service operation.
double value = 102.00D;
client.AddTo(value);
// Call the SubtractFrom service operation.
value = 50.00D;
client.SubtractFrom(value);
// Call the MultiplyBy service operation.
value = 17.65D;
client.MultiplyBy(value);
// Call the DivideBy service operation.
value = 2.00D;
client.DivideBy(value);
client.Addition(5, 9);
client.serviceadd(5, 6, 3);
// Complete equation
client.Clear();
Console.ReadLine();
//Closing the client gracefully closes the connection and cleans up resources
client.Close();
}
}
}
Generatedclient.cs
namespace Microsoft.Samples.DualHttp
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://Microsoft.Samples.DualHttp", ConfigurationName="Microsoft.Samples.DualHttp.ICalculatorDuplex", CallbackContract=typeof(Microsoft.Samples.DualHttp.ICalculatorDuplexCallback), SessionMode=System.ServiceModel.SessionMode.Required)]
public interface ICalculatorDuplex
{
[System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://Microsoft.Samples.DualHttp/ICalculatorDuplex/Clear")]
void Clear();
[System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://Microsoft.Samples.DualHttp/ICalculatorDuplex/AddTo")]
void AddTo(double n);
[System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://Microsoft.Samples.DualHttp/ICalculatorDuplex/SubtractFrom")]
void SubtractFrom(double n);
[System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://Microsoft.Samples.DualHttp/ICalculatorDuplex/MultiplyBy")]
void MultiplyBy(double n);
[System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://Microsoft.Samples.DualHttp/ICalculatorDuplex/DivideBy")]
void DivideBy(double n);
[System.ServiceModel.OperationContractAttribute(IsOneWay = true, Action = "http://Microsoft.Samples.DualHttp/ICalculatorDuplex/Addition")]
void Addition(double a, double b);
[System.ServiceModel.OperationContractAttribute(IsOneWay = true, Action = "http://Microsoft.Samples.DualHttp/ICalculatorDuplex/serviceadd")]
void serviceadd(double a, double b, double c);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface ICalculatorDuplexCallback
{
[System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://Microsoft.Samples.DualHttp/ICalculatorDuplex/Result")]
void Result(double result);
[System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://Microsoft.Samples.DualHttp/ICalculatorDuplex/Equation")]
void Equation(string eqn);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface ICalculatorDuplexChannel : Microsoft.Samples.DualHttp.ICalculatorDuplex, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class CalculatorDuplexClient : System.ServiceModel.DuplexClientBase<Microsoft.Samples.DualHttp.ICalculatorDuplex>, Microsoft.Samples.DualHttp.ICalculatorDuplex
{
public CalculatorDuplexClient(System.ServiceModel.InstanceContext callbackInstance) :
base(callbackInstance)
{
}
public CalculatorDuplexClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName) :
base(callbackInstance, endpointConfigurationName)
{
}
public CalculatorDuplexClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
base(callbackInstance, endpointConfigurationName, remoteAddress)
{
}
public CalculatorDuplexClient(System.ServiceModel.InstanceContext callbackInstance, string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(callbackInstance, endpointConfigurationName, remoteAddress)
{
}
public CalculatorDuplexClient(System.ServiceModel.InstanceContext callbackInstance, System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(callbackInstance, binding, remoteAddress)
{
}
public void Clear()
{
base.Channel.Clear();
}
public void AddTo(double n)
{
base.Channel.AddTo(n);
}
public void SubtractFrom(double n)
{
base.Channel.SubtractFrom(n);
}
public void MultiplyBy(double n)
{
base.Channel.MultiplyBy(n);
}
public void DivideBy(double n)
{
base.Channel.DivideBy(n);
}
public void Addition(double a, double b)
{
base.Channel.Addition(a,b);
}
public void serviceadd(double a, double b, double c)
{
base.Channel.serviceadd(a, b, c);
}
}
}
WCF Service.cs
using System.ServiceModel;
namespace Microsoft.Samples.DualHttp
{
// Define a duplex service contract.
// A duplex contract consists of two interfaces.
// The primary interface is used to send messages from client to service.
// The callback interface is used to send messages from service back to client.
// ICalculatorDuplex allows one to perform multiple operations on a running result.
// The result is sent back after each operation on the ICalculatorCallback interface.
[ServiceContract(Namespace = "http://Microsoft.Samples.DualHttp", SessionMode = SessionMode.Allowed,
CallbackContract = typeof(ICalculatorDuplexCallback))]
public interface ICalculatorDuplex
{
[OperationContract(IsOneWay = true)]
void Clear();
[OperationContract(IsOneWay = true)]
void AddTo(double n);
[OperationContract(IsOneWay = true)]
void SubtractFrom(double n);
[OperationContract(IsOneWay = true)]
void MultiplyBy(double n);
[OperationContract(IsOneWay = true)]
void DivideBy(double n);
[OperationContract(IsOneWay = true)]
void Addition(double a, double b);
[OperationContract(IsOneWay = true)]
void serviceadd(double a, double b, double c);
}
// The callback interface is used to send messages from service back to client.
// The Result operation will return the current result after each operation.
// The Equation opertion will return the complete equation after Clear() is called.
public interface ICalculatorDuplexCallback
{
[OperationContract(IsOneWay = true)]
void Result(double result);
[OperationContract(IsOneWay = true)]
void Equation(string eqn);
}
// Service class which implements a duplex service contract.
// Use an InstanceContextMode of PerSession to store the result
// An instance of the service will be bound to each duplex session
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class CalculatorService : ICalculatorDuplex
{
double result;
string equation;
ICalculatorDuplexCallback callback = null;
public CalculatorService()
{
result = 0.0D;
equation = result.ToString();
callback = OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>();
}
public void Clear()
{
callback.Equation(equation + " = " + result.ToString());
result = 0.0D;
equation = result.ToString();
}
public void AddTo(double n)
{
result += n;
equation += " + " + n.ToString();
callback.Result(result);
}
public void SubtractFrom(double n)
{
result -= n;
equation += " - " + n.ToString();
callback.Result(result);
}
public void MultiplyBy(double n)
{
result *= n;
equation += " * " + n.ToString();
callback.Result(result);
}
public void DivideBy(double n)
{
result /= n;
equation += " / " + n.ToString();
callback.Result(result);
}
public void Addition(double a, double b)
{
double c = a + b;
callback.Result(c);
}
public void serviceadd(double a, double b, double c)
{
CSharpClassLibrary.Class1 object1 = new CSharpClassLibrary.Class1();
double d= object1.add(5, 6, 3);
callback.Result(d);
}
}
}
'CsharpClasslibrary.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpClassLibrary
{
public class Class1
{
public double add(double a, double b, double c)
{
return a + b+ c;
}
}
}
导致问题的方法是“serviceadd”。