0

I created A WCF Application that has 3 methods to return some users Data for test. that It has no Problem I tested. I added the Web Reference in Xamarin But When I want to use the method it has Error Exception.

WCF web.config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.8" />
        <httpRuntime targetFramework="4.8" />
    </system.web>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MyBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="EndpointBehavior">
                    
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service name="MyStoreService.MyStoreService"
               behaviorConfiguration="MyBehavior">
                <endpoint
                    address=""
                    binding="wsHttpBinding"
                    bindingConfiguration="MyBinding"
                    behaviorConfiguration="EndpointBehavior"
                    contract="MyStoreService.IMyStoreService"
                    name="MyStoreService"/>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:58514/MyStoreService.svc" />
                    </baseAddresses>
                </host>
            </service>

        </services>

        <bindings>
            <wsHttpBinding>
                <binding name="MyBinding" >

                </binding>
            </wsHttpBinding>
        </bindings>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />


        <directoryBrowse enabled="true" />
    </system.webServer>
    <entityFramework>
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
    <connectionStrings>
        <add name="ServiceConnection" connectionString="Data Source=.;Initial Catalog=StoreDb;Integrated Security=true;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

this is the Code I Use in Xamarin android with C#

localhost.MyStoreService service = new MyStoreService();
            var user = service.UserGetById(1, true);


            label.Text = user.FName;

And this is the Error Exception

{System.Net.WebException: Error: ConnectFailure (Connection refused) ---> System.Net.Sockets.SocketException: Connection refused  at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00014] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net.Sockets/SocketAsyncResult.cs:127   at System.Net.Sockets.Socket.EndConnect (System.IAsyncResult asyncResult) [0x0002c] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net.Sockets/Socket.cs:1214   at System.Net.WebConnection+<>c.<Connect>b__16_1 (System.IAsyncResult asyncResult) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net/WebConnection.cs:182   at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00019] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs:538 --- End of stack trace from previous location where exception was thrown ---  at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x00197] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net/WebConnection.cs:180    --- End of inner exception stack trace ---  at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x0025d] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net/WebConnection.cs:202   at System.Net.WebConnection.InitConnection (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x000cc] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net/WebConnection.cs:273   at System.Net.WebOperation.Run () [0x00052] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net/WebOperation.cs:268   at System.Net.WebCompletionSource`1[T].WaitForCompletion () [0x0008e] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/System.Net/WebCompletionSource.cs:111   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke (System.String method_name, System.Object[] parameters) [0x000a7] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System.Web.Services/System.Web.Services.Protocols/SoapHttpClientProtocol.cs:364   at MyStoreApplication.localhost.MyStoreService.UserGetById (System.Int64 UId, System.Boolean UIdSpecified) [0x00001] in F:\StoreProject\MyStoreApplication\MyStoreApplication\Web References\localhost\Reference.cs:227   at (wrapper remoting-invoke-with-check) MyStoreApplication.localhost.MyStoreService.UserGetById(long,bool)  at MyStoreApplication.MainActivity.GetData () [0x00007] in F:\StoreProject\MyStoreApplication\MyStoreApplication\MainActivity.cs:147   at MyStoreApplication.MainActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x000aa] in F:\StoreProject\MyStoreApplication\MyStoreApplication\MainActivity.cs:47   at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00010] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-30/mcw/Android.App.Activity.cs:2986   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.3(intptr,intptr,intptr)}

I am new to wcf and xamarin. Please help me to solve the problem?

this is the Pic may help [1]: https://s21.picofile.com/file/8444961626/Error.png

JKP robeg
  • 1
  • 1
  • is your server setup to accept remote connections? – Jason Dec 16 '21 at 16:43
  • i dont know. I just run wcf in debuging mode and add the web service in xamarin project by address (in debug mode, not hosted in iis) – JKP robeg Dec 16 '21 at 17:13
  • the built in VS webserver will not allow remote connections by default. There are **numerous** posts and articles that explain how to change this. – Jason Dec 16 '21 at 17:18
  • what do i have to do to get data from wcf service in xamarin android project ? anybody help me or link to look for? – JKP robeg Dec 16 '21 at 18:36
  • did you fix the problem I already pointed out to you? Xamarin has docs and samples specifically for WCF, have you read them? – Jason Dec 16 '21 at 18:39
  • I searched a little but couldnt find the thing that i want – JKP robeg Dec 16 '21 at 19:26
  • I edited the post and added a pic to explain more. sorry but can you send me some links from docs and article about thing that i want? – JKP robeg Dec 16 '21 at 19:33
  • https://www.google.com/search?q=visual+studio+accept+remote+connections+site:stackoverflow.com – Jason Dec 16 '21 at 19:57
  • https://www.google.com/search?q=xamarin+wcf – Jason Dec 16 '21 at 20:18
  • there are also many newer, better ways of doing webservices than using WCF – Jason Dec 16 '21 at 20:26
  • thank you jason. what you can offer to me? – JKP robeg Dec 16 '21 at 21:19
  • I think this may be because localhost is the loopback address mapped to the device or emulator, not the PC hosting it. You need to use the IP address in the client code.https://stackoverflow.com/questions/31131658/xamarin-connect-to-locally-hosted-web-service && https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/wcf – Lan Huang Dec 17 '21 at 08:14

0 Answers0