none
Hosting WCF Service within WinForm Application doesn't work when running the application executable

    Question


  • Over the past few weeks I have developed a 64-bit WinForms application that needs to communicate with a 32-bit DLL (job specs require it).

    After doing some reading around the internet and finding out that there is not going to be any fun way of doing this, I decided to go with hosting a WCF Service Application within my WinForms application for communicating to the 32-bit DLL... or so I thought I was doing.

    During development (while running within Visual Studio) it has been working really well, but of course, now that I need to deploy, I am running into problems. I am having trouble getting a strong enough understanding of WCF Services to know if I am going about this in a terrible way or if I am just missing some minute detail.

    I created the project as Admin. After development was *finished* I tried to run the WinForm executable (both debug and release), WindowsFormsApplication1.exe. The application started up, but after I tried to complete a task involving the use of the WCF service, an exception was thrown:

    "There was no endpoint listening at http://localhost:44848/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action.

    See InnerException, if present, for more details.

    Unable to connect to the remote server

    No connection could be made because the target machine actively refused it 127.0.0.1:44848"

    This has led me to believe that Visual Studio was doing the hosting of the service during development instead of the WinForm application, or my configs and/or directory structures are incorrect.

    WinForm App.config:

       
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
      </startup>
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IService2" />
            <binding name="BasicHttpBinding_IService3" />
            <binding name="BasicHttpBinding_IService1" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:45053/Service2.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService2" contract="ServiceReference2.IService2" name="BasicHttpBinding_IService2" />
          <endpoint address="http://localhost:46351/Service3.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService3" contract="ServiceReference3.IService3" name="BasicHttpBinding_IService3" />
          <endpoint address="http://localhost:44848/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
        </client>
      </system.serviceModel>
    </configuration>

    WCF Service Web.config:

    <?xml version="1.0"?>
    <configuration>
    
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5.2" />
        <httpRuntime targetFramework="4.5.2" maxRequestLength="2147483647"/>
      </system.web>
    
      <system.net>
        <defaultProxy>
          <proxy usesystemdefault="False"/>
        </defaultProxy>
      </system.net>
      
      <system.diagnostics>
        <sources>
          <source name="System.ServiceModel"
                  switchValue="Information, ActivityTracing"
                  propagateActivity="true" >
            <listeners>
              <add name="xml"/>
            </listeners>
          </source>
          <source name="System.ServiceModel.MessageLogging">
            <listeners>
              <add name="xml"/>
            </listeners>
          </source>
          <source name="myUserTraceSource"
                  switchValue="Information, ActivityTracing">
            <listeners>
              <add name="xml"/>
            </listeners>
          </source>
        </sources>
        <sharedListeners>
          <add name="xml"
               type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData="C:\logs\Traces.svclog" />
        </sharedListeners>
      </system.diagnostics>
    
      <system.serviceModel>
        <diagnostics wmiProviderEnabled="true">
          <messageLogging
               logEntireMessage="true"
               logMalformedMessages="true"
               logMessagesAtServiceLevel="true"
               logMessagesAtTransportLevel="true"
               maxMessagesToLog="3000"
           />
        </diagnostics>
        <behaviors>
          <serviceBehaviors>
            <behavior name="metadadiscovery">
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <services>
          <service name="ServiceReference1.Service1" behaviorConfiguration="metadadiscovery">
            <endpoint address="" binding="basicHttpBinding" contract="ServiceReference1.IService1"></endpoint>
          </service>
        </services>
        
        <protocolMapping>
            <add binding="basicHttpBinding" scheme="http" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    
    </configuration>


    Directory Where EXE resides:

    [Not letting me insert images]

    The directory containing the WCF Service resides within the directory WcfService1 from the image above.

    I have mostly been using the following method of instantiating the service:

        ServiceReference1.Service1Client = new ServiceReference1.SErvice1Client();

    Once I tried to switch to using a service host (below), but when I used that method, the service would timeout whenever it tried to communicate to the DLL.

    Uri address = new Uri("http://localhost:44848/Service1.svc");    
    ServiceHost host = new ServiceHost(typeof(ServiceReference1.Service1Client), address);    
    host.Open();

    And then I closed the host later. At this point, I am willing to try anything to get this working.


    Below is the code of my WindowsFormsApplication1.exe.config file. All three contracts are giving the warning that they're "invalid according to its datatype 'clientContractType'". I think this could be the source of my problems, but I do not know why it is showing this warning:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
      </startup>
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IService2" />
            <binding name="BasicHttpBinding_IService3" />
            <binding name="BasicHttpBinding_IService1" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:45053/Service2.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService2" contract="ServiceReference2.IService2" name="BasicHttpBinding_IService2" />
          <endpoint address="http://localhost:46351/Service3.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService3" contract="ServiceReference3.IService3" name="BasicHttpBinding_IService3" />
          <endpoint address="http://localhost:44848/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
        </client>
      </system.serviceModel>
    </configuration>


    Thank you for any help and guidance you can provide.




    • Edited by bhaan9 Thursday, December 07, 2017 7:41 PM
    Thursday, December 07, 2017 7:21 PM

Answers

  • Hi bhaan9,

    >>I created the project as Admin

    What do you mean by this? Do you mean you run VS under Administrator, and then create the winform project? I suggest you run WindowsFormsApplication1.exe under Administrator.

    >>This has led me to believe that Visual Studio was doing the hosting of the service during development instead of the WinForm application, or my configs and/or directory structures are incorrect

    How did you create and host WCF Service? In general, Service.svc is under WCF Service Application instead of Winform application, in Winform, we implement WCF Service by WCF library and host it. There should not be web.config when you host in winform.

    >>Once I tried to switch to using a service host (below), but when I used that method, the service would timeout whenever it tried to communicate to the DLL.

    This is totally incorrect. I suggest you refer link below for hosting WCF in winform.

    #How to: Host a WCF Service in a Managed Application

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-host-a-wcf-service-in-a-managed-application

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by bhaan9 Friday, December 08, 2017 6:03 PM
    Friday, December 08, 2017 1:48 AM
    Moderator

All replies

  • Hi bhaan9,

    >>I created the project as Admin

    What do you mean by this? Do you mean you run VS under Administrator, and then create the winform project? I suggest you run WindowsFormsApplication1.exe under Administrator.

    >>This has led me to believe that Visual Studio was doing the hosting of the service during development instead of the WinForm application, or my configs and/or directory structures are incorrect

    How did you create and host WCF Service? In general, Service.svc is under WCF Service Application instead of Winform application, in Winform, we implement WCF Service by WCF library and host it. There should not be web.config when you host in winform.

    >>Once I tried to switch to using a service host (below), but when I used that method, the service would timeout whenever it tried to communicate to the DLL.

    This is totally incorrect. I suggest you refer link below for hosting WCF in winform.

    #How to: Host a WCF Service in a Managed Application

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-host-a-wcf-service-in-a-managed-application

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by bhaan9 Friday, December 08, 2017 6:03 PM
    Friday, December 08, 2017 1:48 AM
    Moderator
  • Over the past few weeks I have developed a 64-bit WinForms application that needs to communicate with a 32-bit DLL (job specs require it).

    .NET 64bit solution can consume a 32bit solution. So, I don't see the purpose of even using WCF. 

    Friday, December 08, 2017 9:49 AM
  • Doing this came with huge advantages:

    1. No configuration file was necessary
    2. Running it this way must've gotten rid of a large amount of overhead because the communication to the DLL (what I am using the service for) used to take several seconds to do large amounts of calls to the DLL, but now is able to handle 10k+ calls in the blink of an eye.
    3. No service reference is necessary. I just needed the file for my main function, the file containing the service function implementations and the file containing the interface for the service.

    I've never done anything like this before so I was trying all sorts of different things to make it work. Turned out to be way simpler than what I was making it... as usual.


    Thanks for the help. The simple guidance of saying I was doing it wrong, do it this way got me where I needed to go.

    Friday, December 08, 2017 6:06 PM
  • Would you mind explaining (or linking me too an explanation) what it would be to consume a 32 bit solution? The solution above requires that I run the executable as administrator and if there is a solution where that is not necessary then that would be the preferred solution. 

    Thank you for your help.

    Friday, December 08, 2017 6:16 PM
  • Hi bhaan9,

    Since your current issue is related with consumeing a 32 dll from 64 bit app, I would suggest you post a new thread, and then we could focus on this new issue.

    Regards,

    Tony


    Help each other

    9 hours 7 minutes ago