【问题标题】:Creating a WCF Client Inheriting from ClientBase in F#在 F# 中创建从 ClientBase 继承的 WCF 客户端
【发布时间】:2011-05-26 01:54:11
【问题描述】:

我在 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

【问题讨论】:

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


    【解决方案1】:

    这是一个粗略的尝试:

    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)
    

    【讨论】:

    • 一种需要使用非主构造函数 F# 类语法的罕见情况 - 当您希望拥有多个构造函数,每个构造函数都调用不同的基类构造函数时。
    • @Brian - 我想这就是我对这个感到困惑的原因。
    • @kvb - 你总是让它看起来很简单!再次感谢您的帮助!
    • @Beaker - 没问题,很乐意提供帮助。
    • @kvb - 当我编写这段代码时: let client = new CalculatorClient() let addValue = client.Add(3,5) 我得到“构造函数或方法 Add 未定义”。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 2021-02-24
    相关资源
    最近更新 更多