Не удалось инициализировать систему WCF C # с - PullRequest
0 голосов
/ 18 ноября 2018

enter image description here Я не могу разрешить приведенную ниже ошибку. Произошло необработанное исключение типа «System.TypeInitializationException» в System.ServiceModel.dll. какие-либо предложения?

    using (ServiceHost host=new ServiceHost(typeof(HelloService.HelloService)))
    {
        host.Open();
        Console.WriteLine("host started @" + DateTime.Now);
        Console.ReadLine();
    }
    stack trace for the error is mentioned below 
    `        at System.ServiceModel.Diagnostics.TraceUtility.SetEtwProviderId()
    at System.ServiceModel.ServiceHostBase..ctor()
    at System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses)
    at HelloServiceHost.Program.Main(String[] args) in E:\WCF\HelloService\HelloServiceHost\Program.cs:line 14
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart(

1 Ответ

0 голосов
/ 19 ноября 2018

Я сделал демо и обнаружил, что во фрагменте кода нет проблем, я предлагаю вам опубликовать более подробную информацию о вашем проекте.
Вот мой консольный проект, желаю, чтобы он был вам полезен.
Сервер.

class Program
{
    static void Main(string[] args)
    {

        using (ServiceHost sh=new ServiceHost(typeof(MyService)))
        {
            sh.Open();
            Console.WriteLine("Service is ready....");

            Console.ReadLine();
            sh.Close();
        }
    }
}
[ServiceContract]
public interface IService
{
    [OperationContract]
    int Mult(int x, int y);
}
public class MyService : IService
{

    public int Mult(int x, int y)
    {
        OperationContext oc = OperationContext.Current;

        Console.WriteLine(oc.Channel.LocalAddress.Uri);
        return x * y;
    }       
 }

App.config

<system.serviceModel>
    <services>
      <service name="WCFServer1.MyService" behaviorConfiguration="mybehavior">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="WCFServer1.IService"></endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="WCFServer1.IService"></endpoint>
        <host>
        <baseAddresses>
          <add baseAddress="http://localhost:5110"/>
          <add baseAddress="net.tcp://localhost:5120"/>
        </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mybehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Результат. enter image description here Не стесняйтесь, дайте мне знать, если есть что-то, с чем я могу помочь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...