Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mscorlib.dll中出现“System.ServiceModel.CommunicationObjectFaultedException”_C#_.net_Wcf_Service_Class Library - Fatal编程技术网

C# mscorlib.dll中出现“System.ServiceModel.CommunicationObjectFaultedException”

C# mscorlib.dll中出现“System.ServiceModel.CommunicationObjectFaultedException”,c#,.net,wcf,service,class-library,C#,.net,Wcf,Service,Class Library,我的解决方案中有3个项目,基本上是两个数字相加,然后将值返回给客户端。我想用WCF中的这个简单项目完成从服务到客户机的回调 C控制台应用程序,作为客户端,通过提供要添加的2个值来调用WCF服务库。 WCF服务库(即服务)调用C类库进行添加。 WCF服务库正在调用C类库来添加这两个值,它执行添加这两个值的工作,并将值返回到WCF服务库,WCF服务库将值返回到C控制台应用程序。 WCF服务接口的配置和实现该接口的WCF服务类如下 当作为C控制台应用程序的客户端调用WCF服务时,如果WCF服务进行添加

我的解决方案中有3个项目,基本上是两个数字相加,然后将值返回给客户端。我想用WCF中的这个简单项目完成从服务到客户机的回调

C控制台应用程序,作为客户端,通过提供要添加的2个值来调用WCF服务库。 WCF服务库(即服务)调用C类库进行添加。 WCF服务库正在调用C类库来添加这两个值,它执行添加这两个值的工作,并将值返回到WCF服务库,WCF服务库将值返回到C控制台应用程序。 WCF服务接口的配置和实现该接口的WCF服务类如下

当作为C控制台应用程序的客户端调用WCF服务时,如果WCF服务进行添加并返回控制台应用程序,则我的项目工作正常

若客户机调用服务,而服务又调用一个C类库来计算加法,它会给出“System.ServiceModel.CommunicationObjectFaultedException发生在mscorlib.dll中”

WCF服务调用C类库的方式是在console应用程序调用的方法内创建C类库的实例,并使用该实例调用该库的方法

我想我得想个办法把会议安排好。如果我错了,请纠正我,这样不会使通信对象处于错误状态。但我不知道怎么做。除此之外,我们也非常欣赏其他想法。提前谢谢大家

Client.cs

导致问题的方法是serviceadd


请添加在创建wcf客户端并关闭它时引发异常的代码片段。我已经添加了所有项目,导致问题的方法是servicadd方法。请看一下,帮帮我。我真的很感激你的帮助。
 [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession) ServiceContract[(SessionMode = SessionMode.Required.)]
 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();
    }
}
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);
    }

}
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);
    }

}
    }
     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;
                 }
           }         
     }