在 F# 中创建从 ClientBase 继承的 WCF 客户端
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个粗略的尝试:
Here's a rough stab at it: