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

发布于 2024-11-09 11:59:48 字数 3395 浏览 0 评论 0原文

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

I found the following code on the msdn site here:
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);
    }
}

Obviously, this is the world famous WCF CalculatorService on msdn. The code I have shown above was generated with svcutil.exe. However, svcutil.exe does not generate F# code so I'm trying to translate CalculatorClient into F# (I can handle the interfaces just fine). I would show you what I have so far, except I only have the first three lines completed and those are probably wrong anyhow (I get that impression from all the red squigglyness decorating it). I'm completely stumped by the combination of inheriting from a base class and an interface at the same time. I don't need the whole class translated, just the class with a few constructors and one of the interface methods, and I'm sure I can work out the rest. Although, if you want to translate the whole thing for posterity please feel free.

Thanks,

Bob

Edit: Here is the interface I am using. Feel free to edit it if it could be improved.

[<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2024-11-16 11:59:48

这是一个粗略的尝试:

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)

Here's a rough stab at it:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文