在F#中创建从ClientBase继承的WCF客户端

时间:2011-05-26 01:54:11

标签: wcf inheritance f# c#-to-f#

我在msdn网站上找到了以下代码: http://msdn.microsoft.com/en-us/library/ms733133.aspx#Y380

[System.ServiceModel.ServiceContractAttribute(Namespace="http://Microsoft.ServiceModel.Samples", ConfigurationName="ICalculator")]
public interface ICalculator
{
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
    double Add(double n1, double n2);

    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
    double Subtract(double n1, double n2);

    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
    double Multiply(double n1, double n2);

    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
    double Divide(double n1, double n2);
}

public interface ICalculatorChannel : ICalculator, System.ServiceModel.IClientChannel
{
}

public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, ICalculator
{

    public CalculatorClient()
    {
    }

    public CalculatorClient(string endpointConfigurationName) : 
            base(endpointConfigurationName)
    {
    }

    public CalculatorClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public CalculatorClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public CalculatorClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }

    public double Add(double n1, double n2)
    {
        return base.Channel.Add(n1, n2);
    }

    public double Subtract(double n1, double n2)
    {
        return base.Channel.Subtract(n1, n2);
    }

    public double Multiply(double n1, double n2)
    {
        return base.Channel.Multiply(n1, n2);
    }

    public double Divide(double n1, double n2)
    {
        return base.Channel.Divide(n1, n2);
    }
}

显然,这是msdn上世界着名的WCF CalculatorService。我上面显示的代码是使用svcutil.exe生成的。但是,svcutil.exe不生成F#代码,所以我试图将CalculatorClient转换为F#(我可以正常处理接口)。我会告诉你到目前为止我所拥有的东西,除了我只完成了前三行并且无论如何都可能是错误的(我从所有装饰它的红色波浪中得到了印象)。我完全被同时继承基类和接口的组合所困扰。我不需要翻译整个类,只需要一些构造函数和一个接口方法的类,我相信我可以解决其余的问题。虽然,如果你想为后代翻译整件事,请随意。

谢谢,

鲍勃

编辑:这是我正在使用的界面。如果可以改进,请随意编辑。

[<ServiceContract>]  
type ICalculator =
  [<OperationContract>]
  abstract member Add: float*float -> float
  [<OperationContract>]
  abstract member Subtract: float*float -> float
  [<OperationContract>]
  abstract member Multiply: float*float -> float
  [<OperationContract>]
  abstract member Divide: float*float -> float

1 个答案:

答案 0 :(得分:4)

这是一个粗略的尝试:

type CalculatorClient =
    inherit System.ServiceModel.ClientBase<ICalculator>
    new() = 
        { inherit System.ServiceModel.ClientBase<ICalculator>() }
    new(name:string) = 
        { inherit System.ServiceModel.ClientBase<ICalculator>(name) }
    new(name:string, remoteAddress:string) = 
        { inherit System.ServiceModel.ClientBase<ICalculator>(name, remoteAddress) }
    new(name:string, remoteAddress:System.ServiceModel.EndpointAddress) = 
        { inherit System.ServiceModel.ClientBase<ICalculator>(name, remoteAddress) }
    new(binding:System.ServiceModel.Channels.Binding, remoteAddress) = 
        { inherit System.ServiceModel.ClientBase<ICalculator>(binding, remoteAddress) }
    member x.Add(n1,n2) = base.Channel.Add(n1,n2)
    member x.Subtract(n1,n2) = base.Channel.Subtract(n1,n2)
    member x.Multiply(n1,n2) = base.Channel.Multiply(n1,n2)
    member x.Divide(n1,n2) = base.Channel.Divide(n1,n2)
    interface ICalculator with
        // call into the methods on the class - these are *not* recursive
        member x.Add(n1,n2) = x.Add(n1,n2)
        member x.Subtract(n1,n2) = x.Subtract(n1,n2)
        member x.Multiply(n1,n2) = x.Multiply(n1,n2)
        member x.Divide(n1,n2) = x.Divide(n1,n2)