如何修复“无法将MWArray转换为请求的类型”

时间:2019-01-02 20:40:35

标签: c# matlab wcf

我开始为Matlab应用程序设置WCF服务器,并希望使用Microsoft WCF Test Client接收结构。我需要帮助将Matlab结构成功传回。

我正在遵循MATHWORKS提供的说明:

https://www.mathworks.com/help/compiler_sdk/dotnet/create-windows-communications-foundation-based-components.html#bsuwtjv

我正在使用Windows 10 VS 2017社区版。对于Matlab,我正在使用:

<MathWorks_version_info>
  <version>9.5.0.944444</version>
  <release>R2018b</release>
  <description></description>
  <date>Aug 28 2018</date>
  <checksum>1708982227</checksum>

我已经能够使用简单的字符串回复在WCF上成功进行通信。我可以将数据传递到Matlab,并生成一个简单的图形。但是,当我尝试通过结构使用Matlab返回时,我似乎无法摆脱下面描述的错误。

Matlab功能:

function y = callMATLABfunction( x )
    clearvars -except x; close all;
    if nargin == 1 && ~isempty(x)
        if isnumeric(x)

       [...]

           y.Completion = 1;
           y.Result = 'FirstCompletion';
           y.Message = 'Success';

        else

        [...]          

           y.Completion = 1;
           y.Result = 'SecondCompletion';
           y.Message = 'Success';

        end
    else
        y.Completion = 0;
        y.Result = 'Error';
        y.Message = 'Please supply one input variable';
    end
end

IMatlabInterface.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using System.Text;
using System.Threading.Tasks;

namespace MatlabSvc
{
    [ServiceContract]
    public interface IMatlabInterface
    {
        [OperationContract(Name = "callMATLABfunction_1")]
        y callMATLABfunction(System.Double x);
        [OperationContract(Name = "callMATLABfunction_2")]
        y callMATLABfunction(System.String x);
    }
    [KnownType(typeof(y))]
    [DataContract]
    public class y
    {
        [DataMember]
        public double Completion { get; set; }
        [DataMember]
        public string Result { get; set; }
        [DataMember]
        public string Message { get; set; }
    }
}

我希望在Microsoft WCF测试客户端中看到返回结构。相反,我得到了:

无法将MWArray转换为请求的类型

服务器堆栈跟踪:    在System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(消息回复,MessageFault错误,字符串操作,MessageVersion版本,FaultConverter faultConverter)    在System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc&rpc)    在System.ServiceModel.Channels.ServiceChannel.Call处(字符串操作,布尔单向,ProxyOperationRuntime操作,Object [] ins,Object [] outs,TimeSpan超时)    在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)    在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)处

异常重新抛出为[0]:    在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)    在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型)    在IMatlabInterface.callMATLABfunction_1(Double x)    在MatlabInterfaceClient.callMATLABfunction_1(Double x)

0 个答案:

没有答案