Android 我无法与Xamarin中的WCF服务建立连接

Android 我无法与Xamarin中的WCF服务建立连接,android,wcf,xamarin,xamarin.forms,wcf-binding,Android,Wcf,Xamarin,Xamarin.forms,Wcf Binding,我的老同事已经在VB.NET中开发了一个WPF应用程序。现在,我的上司希望通过实现在Android系统上运行的相同概念来取代当前的平台。所以,我决定使用XamarinAndroid.NET标准 在我询问之前,我已经寻找了一种使用WCF和错误抛出的方法,但我仍然被相同的错误所困扰 我已经在android类上设置了服务引用WCF。根据解决方案,我有两个项目:-MyProject和MyProject.Android ===========================================

我的老同事已经在VB.NET中开发了一个WPF应用程序。现在,我的上司希望通过实现在Android系统上运行的相同概念来取代当前的平台。所以,我决定使用XamarinAndroid.NET标准

在我询问之前,我已经寻找了一种使用WCF和错误抛出的方法,但我仍然被相同的错误所困扰

我已经在android类上设置了服务引用WCF。根据解决方案,我有两个项目:-MyProjectMyProject.Android

===========================================================================

WPF

Private Function EstablishConnect() As Boolean
    Try
        Dim context As InstanceContext = New InstanceContext(Me)
        svrobj = New DisplayServicesClient(context, "DisplayTcpBinding")
        Dim endpointAddress As New EndpointAddress(modUtility.ServerEndPointAddress)
        svrobj.Endpoint.Address = endpointAddress
        svrobj.Open()
        SecondDiffFromServer = DateDiff(DateInterval.Second, Now, svrobj.GetDateTime)
        Return True
    Catch ex As Exception
        WriteLog.Log(ex.ToString, True)
        Return False
    End Try
End Function
modUtility.ServerEndPointAddress从INI获取:

净。tcp://localhost:8002/AppServ/WCFServices/Display

===========================================================================

XAMARIN形式

private async Task<bool> EstablishConnectAsync()
{
    try
    {
        InstanceContext context = new InstanceContext(this);
        svrobj = new DisplayServicesClient(DisplayServicesClientBase.EndpointConfiguration.DisplayTcpBinding); //, DisplayServicesClientBase.EndpointConfiguration.DisplayTcpBinding
        EndpointAddress endpointAddress = new EndpointAddress(modUtility.ServerEndPointAddress);
        svrobj.Endpoint.Address = endpointAddress;
        await svrobj.OpenAsync();
        modUtility.SecondDiffFromServer = (int)(DateTime.Now - await svrobj.GetDateTimeAsync()).TotalSeconds;
        return true;
    }
    catch (Exception ex)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            ResultText.Text = "Unable to established connection with endpoint service. Ex-" + ex.Message;
        });
        return false;
    }
}
专用异步任务
{
尝试
{
InstanceContext上下文=新InstanceContext(此);
svrobj=new DisplayServicesClient(DisplayServicesClientBase.EndpointConfiguration.DisplayTcpBinding);/,DisplayServicesClientBase.EndpointConfiguration.DisplayTcpBinding
EndpointAddress EndpointAddress=新的EndpointAddress(modUtility.ServerEndPointAddress);
svrobj.Endpoint.Address=端点地址;
等待svrobj.OpenAsync();
modUtility.SecondDiffFromServer=(int)(DateTime.Now-wait svrobj.GetDateTimeAsync()).TotalSeconds;
返回true;
}
捕获(例外情况除外)
{
Device.beginInvokeMainThread(()=>
{
ResultText.Text=“无法与endpoint service.Ex建立连接-”+Ex.Message;
});
返回false;
}
}
modUtility.ServerEndPointAddress从设置获取:

净。tcp://10.0.2.2:8002/AppServ/WCFServices/Display

我目前在emulator上运行android

我的代码在调用
svrobj
函数时抛出异常。下面是抛出的错误:-

  • 通信对象System.ServiceModel.DuplexChannelFactory`1[AppServ.IDisplayServices]未处于创建状态,而是处于打开状态

  • 未实现该方法或操作


  • 我是否遗漏了某些内容或实现方式有误?

    我认为使用HTTPClient的服务是一种更好的方法,如果您想在xamarin.forms中使用wcf,您可以参考@LeoZhu MSFT。我的方向是目前可以使用任何有效的服务,而不是继续使用wcf。目前,我们的WCF可以建立连接,客户端将等待来自web服务的任何命令,而无需继续请求。我不确定,但是restful可以这样做吗?也就是说,已建立连接并等待命令,但没有在特定时间间隔内保持POST/GET到web服务。restful无法继续请求,正如您所说的,也许您可以参考,在Xamarin.Forms中有一个使用WCF的示例。