简体   繁体   English

Xamarin 表单 ios 在 iOS 中调用 Web 服务时出现异常

[英]Xamarin form ios get exception when calling web service in iOS

I got an exception when calling web API in Xamarin ios(android is working fine), My step is在 Xamarin ios 中调用 Web API 时出现异常(android 工作正常),我的步骤是

  1. use svcutil.exe to generate the config and C# file使用 svcutil.exe 生成配置和 C# 文件
<!--config file-->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IOrderApi" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://40.115.139.190:9002/OrderApi.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IOrderApi" contract="IOrderApi"
                name="BasicHttpBinding_IOrderApi" />
        </client>
    </system.serviceModel>
</configuration>

/* C# */
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IOrderApi")]
public interface IOrderApi
{
...    
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IOrderApi/GetLoginToken", ReplyAction="http://tempuri.org/IOrderApi/GetLoginTokenResponse")]
    string GetLoginToken(string args);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IOrderApi/GetLoginToken", ReplyAction="http://tempuri.org/IOrderApi/GetLoginTokenResponse")]
    System.Threading.Tasks.Task<string> GetLoginTokenAsync(string args);

...
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IOrderApiChannel : IOrderApi, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class OrderApiClient : System.ServiceModel.ClientBase<IOrderApi>, IOrderApi
{

    public OrderApiClient()
    {
    }

    public string GetLoginToken(string args)
    {
        return base.Channel.GetLoginToken(args);
    }

    public System.Threading.Tasks.Task<string> GetLoginTokenAsync(string args)
    {
        return base.Channel.GetLoginTokenAsync(args);
    }
}
  1. call web api GetLoginToken in Xamarin form以 Xamarin 形式调用 web api GetLoginToken
/* Xamarin Form */
private void DoProcess()
{
    LoginData loginData = new LoginData();

    loginData.StaffCorpCode = CompanyEdit.Text;
    loginData.StaffId = MemberEdit.Text;
    loginData.StaffPassword = PasswordEdit.Text;

    OrderApiClient client = new OrderApiClient(new BasicHttpBinding(), new EndpointAddress("http://40.115.139.190:9002/OrderApi.svc"));

    string loginReturn = client.GetLoginToken(JsonConvert.SerializeObject(loginData));
}

Will get exception in form when calling client.调用客户端时会在表单中出现异常。 GetLoginToken (only in ios, android no issue). GetLoginToken (仅在 ios 中,android 没有问题)。 the exception is MonoTouch does not support dynamic proxy code generation.例外是MonoTouch 不支持动态代理代码生成。 Override this method or its caller to return specific client proxy instance .覆盖此方法或其调用者以返回特定的客户端代理实例 How to fix this issue?如何解决这个问题?

So, there are some issue with WcfCore and Mono in iOS.因此,iOS 中的 WcfCore 和 Mono 存在一些问题。 For example it can not generate a dynamic proxi.例如,它不能生成动态代理。 If you will see your code, you will find out that IOrderApiChannel has no implementations.如果你看到你的代码,你会发现 IOrderApiChannel 没有实现。 In WCF core this will creates in runtime.在 WCF 核心中,这将在运行时创建。 But in youe case you should implement it by hands.但在你的情况下,你应该手动实现它。

ChannelFactory has a virtual method CreateChannel(). ChannelFactory 有一个虚方法 CreateChannel()。 If this is not overridden, it uses dynamic code generation, which fails on MonoTouch.如果这不被覆盖,它将使用动态代码生成,这在 MonoTouch 上失败。

The solution is to override it and provide your own compile-time implementation.解决方案是覆盖它并提供您自己的编译时实现。 Here the good example: File chat generated by svc util https://github.com/sami1971/SimplyMobile/blob/master/Core/Plugins/Stocks/SimplyMobile.Plugins.WcfStockService/StockQuoteServiceClient.cs这是一个很好的例子:svc util 生成的文件聊天https://github.com/sami1971/SimplyMobile/blob/master/Core/Plugins/Stocks/SimplyMobile.Plugins.WcfStockService/StockQuoteServiceClient.cs

File that created by hands: https://github.com/sami1971/SimplyMobile/blob/master/Core/Plugins/Stocks/SimplyMobile.Plugins.WcfStockService/StockQuoteServiceClient.iOS.cs手动创建的文件: https : //github.com/sami1971/SimplyMobile/blob/master/Core/Plugins/Stocks/SimplyMobile.Plugins.WcfStockService/StockQuoteServiceClient.iOS.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM