动态创建Web引用

时间:2015-08-24 20:14:42

标签: c# android web-services xamarin

我正在使用c#在Xamarin Android上开发Android应用程序。  有没有办法动态更改SOAP Web服务的URL?我想将url存储在某种配置文件中,但我不知道它是怎么做的。

2 个答案:

答案 0 :(得分:1)

纠正我,如果我错了

但是xamarin-soap webservice类的第二个构造函数具有URL属性。

这是我的网络服务的一个例子:

public partial class Service : System.Web.Services.Protocols.SoapHttpClientProtocol 
    {                    
                public Service() 
                {
                    this.Url = "http://xxx/service.asmx";
                }

                public Service(string url) 
                {
                    this.Url = url;
                }
    }

您已在xamarin中添加了网络参考,然后使用您的网络服务 - 实例。

只需调用第二个构造函数,并为其提供另一个URL作为源。

答案 1 :(得分:1)

您可以使用Channelfactory手动创建所需的连接,并根据需要进行更改。您需要在web.config文件中使用正确的连接。

这是您可以设置的方式。

web.config

 <appSettings>
    <add key="Identity" value="machineidentity" />
    <add key="Binding" value="WSHttpBinding_IService" />
    <add key="Endpoint" value="http://Devservice/Service.svc" />
    <add key="Identity2" value="localmachine" />
    <add key="Binding2" value="WSHttpBinding_IService" />
    <add key="Endpoint2" value="http://Devservice/Service.svc" />
    <add key="devIdentity" value="localmachine" />
    <add key="devBinding" value="WSHttpBinding_IService" />
    <add key="devEndpoint" value="http://Devservice/Service.svc" />
  </appSettings>

C#代码

用于保存来自web.config

的值的配置类
public static Dictionary<int, Connections> EndpointConnections = new Dictionary<int, Connections>
    {
        {1, new Connections(){Identity = ConfigurationManager.AppSettings["Identity"],Binding = ConfigurationManager.AppSettings["Binding"], Endpoint = ConfigurationManager.AppSettings["Endpoint"]}},
        {2, new Connections(){Identity = ConfigurationManager.AppSettings["Identity2"],Binding = ConfigurationManager.AppSettings["Binding2"], Endpoint = ConfigurationManager.AppSettings["Endpoint2"]}},
        {3, new Connections(){Identity = ConfigurationManager.AppSettings["devIdentity"],Binding = ConfigurationManager.AppSettings["devBinding"], Endpoint = ConfigurationManager.AppSettings["devEndpoint"]}},
   };

现在是一个用于创建端点的静态类

private ChannelFactory<IService> SetChannelFactory(int configInput)
            {
                var identity = EndpointIdentity.CreateDnsIdentity(Configuration.EndpointConnections[configInput].Identity);
                var myBinding = new WSHttpBinding(Configuration.EndpointConnections[configInput].Binding);
                var myuri = new Uri(Configuration.EndpointConnections[configInput].Endpoint);
                var myEndpoint = new EndpointAddress(myuri, identity);

                return new ChannelFactory<IService>(myBinding, myEndpoint);
            }

现在调用并使用端点

public async Task<Result> SomeAction(int selection)
{
        IService client = null;
        Result result = null;
        Response response = null;

        using (var myChannelFactory = SetChannelFactory(selection))
        {
             try
             {
                  client = myChannelFactory.CreateChannel();
                  response = await client.TheServiceFunction().ConfigureAwait(false);
                  ((ICommunicationObject)client).Close();
             }
             catch
             {
                  if (client != null)
                  {
                       ((ICommunicationObject)client).Abort();
                       return new result ( failure = true);
                  }
             }
        }
        if (response != null)
        {
                 //Whatever you want to do with the response here
                 return new result ( failure = false);
        }
}