Вопрос

I have to consume a WCF service hosted in Windows Service using a COM Visible assembly.

I have a WCF Service that is hosted in Windows Service, I have to consume the service in a COM Visible assembly, I have created a COM + application and added a service reference to it. Below is the app.config.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="TcpEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
                    transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="None">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://serverMachine:9600/DocumentsWcfService/Tcp"
                binding="netTcpBinding" bindingConfiguration="TcpEndpoint"
                contract="MysWcfService.IMysWcfService" name="TcpEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

When I use this assembly in a classic ASP project and call the method that initializes the service I get the error as below. But when I refer the same WCF service in Console application, it works fine. Is there any specific changes required to consume in COM visible application.

Could not find default endpoint element that references contract 'MyWcfService.IMysWcfService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element

Это было полезно?

Решение

COM server don't read normal config files so you either need to do it via code or use the following trick:

(Your COM server must be an .exe)

  1. Go to dcomcnfg and create a COM+ application
  2. In the COM+ application there is a setting "Application Root Directory". Enter the folder where your .exe is located
  3. Add your com object to the COM+ application
  4. Create a file named Application.manifest in the folder. The file must contain:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" />

  5. Put your config file in the same folder. Note that the config file must have the exact name Application.config, not "myapp.exe.config"

Now the config file will work the same way as in your console application.

Другие советы

Try to read configuration with

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "MysWcfService.dll.config";
configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

Then you can try to read some wcf config sections and configure your host programmatically (example):

    ServicesSection servicesSection = (ServicesSection)configuration.GetSection("system.serviceModel/services");
    ServiceEndpointElement endpoint = servicesSection.Services[0].Endpoints[0];
    //use endpoint.Address                                                                                            
    //use endpoint.Binding
    //use endpoint.Contract
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top