c# - Exception in self host wcf service - The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception -


i have got following exception, when tried port existing iis-based rest service self-hosted one.

the type initializer 'system.servicemodel.diagnostics.traceutility' threw exception.

i following self-hosted wcf rest service port existing code, before testing sample code given in this.

my inner exception given below.

enter image description here

now after commenting out whole <configsections> tag, throws exception:

the contract name 'selfhostedrestservice.iservice' not found in list of contracts implemented service 'service'.

here configuration file:

<?xml version="1.0"?> <configuration> <startup><supportedruntime version="v4.0" sku=".netframework,version=v4.0"/></startup>    <!--<configsections>     <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net" />     <sectiongroup name="system.web.extensions" type="system.web.configuration.systemwebextensionssectiongroup, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35">       <sectiongroup name="scripting" type="system.web.configuration.scriptingsectiongroup, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35">         <section name="scriptresourcehandler" type="system.web.configuration.scriptingscriptresourcehandlersection, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35" requirepermission="false" allowdefinition="machinetoapplication"/>         <sectiongroup name="webservices" type="system.web.configuration.scriptingwebservicessectiongroup, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35">           <section name="jsonserialization" type="system.web.configuration.scriptingjsonserializationsection, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35" requirepermission="false" allowdefinition="everywhere"/>           <section name="profileservice" type="system.web.configuration.scriptingprofileservicesection, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35" requirepermission="false" allowdefinition="machinetoapplication"/>           <section name="authenticationservice" type="system.web.configuration.scriptingauthenticationservicesection, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35" requirepermission="false" allowdefinition="machinetoapplication"/>           <section name="roleservice" type="system.web.configuration.scriptingroleservicesection, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35" requirepermission="false" allowdefinition="machinetoapplication"/>         </sectiongroup>       </sectiongroup>     </sectiongroup>   </configsections>-->    <!--<log4net>     <appender type="log4net.appender.rollingfileappender" name="debuglog">       <file value="c:\\testproj\\info.txt" />       <layout type="log4net.layout.patternlayout">         <conversionpattern value="%d [%t] %-5p %c - %m%n" />       </layout>     </appender>     <appender type="log4net.appender.rollingfileappender" name="restservicelog">       <file value="c:\\testproj\\error.txt" />       <layout type="log4net.layout.patternlayout">         <conversionpattern value="%d [%t] %-5p %c - %m%n" />       </layout>     </appender>     <appender type="log4net.appender.rollingfileappender" name="bmitwarninglog">       <file value="c:\\testproj\\pingresult.txt" />       <layout type="log4net.layout.patternlayout">         <conversionpattern value="%d [%t] %-5p %c - %m%n" />       </layout>     </appender>     <logger name="debugloggerclass">       <level value="debug" />       <appendtofile value="true" />       <rollingstyle value="size" />       <maxsizerollbackups value="10" />       <maximumfilesize value="1024kb" />       <staticlogfilename value="true" />       <param value="debug" name="threshold" />       <appender-ref ref="debuglog" />     </logger>     <logger name="selfhostedrestservice">       <level value="error" />       <appendtofile value="true" />       <rollingstyle value="size" />       <maxsizerollbackups value="10" />       <maximumfilesize value="1024kb" />       <staticlogfilename value="true" />       <param value="error" name="threshold" />       <appender-ref ref="restservicelog" />     </logger>     <logger name="warningloggerclass">       <level value="warn" />       <appendtofile value="true" />       <rollingstyle value="size" />       <maxsizerollbackups value="10" />       <maximumfilesize value="1024kb" />       <staticlogfilename value="true" />       <param value="warn" name="threshold" />       <appender-ref ref="bmitwarninglog" />     </logger>   </log4net>-->    <appsettings/>   <connectionstrings/>   <system.web>     <webservices>       <protocols>         <add name="httpsoap" />         <add name="httpget" />         <add name="httppost" />       </protocols>     </webservices>      <compilation debug="true">       <assemblies>         <add assembly="system.core, version=4.0, culture=neutral, publickeytoken=b77a5c561934e089"/>         <add assembly="system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>       </assemblies>     </compilation>      <authentication mode="windows"/>      <pages>       <controls>         <add tagprefix="asp" namespace="system.web.ui" assembly="system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>       </controls>     </pages>     <httphandlers>       <remove verb="*" path="*.asmx"/>       <add verb="*" path="*.asmx" validate="false" type="system.web.script.services.scripthandlerfactory, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>       <add verb="*" path="*_appservice.axd" validate="false" type="system.web.script.services.scripthandlerfactory, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>       <add verb="get,head" path="scriptresource.axd" type="system.web.handlers.scriptresourcehandler, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35" validate="false"/>     </httphandlers>     <httpmodules>       <add name="scriptmodule" type="system.web.handlers.scriptmodule, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>     </httpmodules>     <identity impersonate="false" />   </system.web>   <system.codedom>     <compilers>       <compiler language="c#;cs;csharp" extension=".cs" warninglevel="4" type="microsoft.csharp.csharpcodeprovider, system, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089">         <provideroption name="compilerversion" value="v4.0"/>         <provideroption name="warnaserror" value="false"/>       </compiler>     </compilers>   </system.codedom>    <system.webserver>     <validation validateintegratedmodeconfiguration="false"/>     <modules>       <add name="scriptmodule" precondition="integratedmode" type="system.web.handlers.scriptmodule, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>     </modules>     <handlers>       <remove name="webservicehandlerfactory-integrated"/>       <add name="scripthandlerfactory" verb="*" path="*.asmx" precondition="integratedmode" type="system.web.script.services.scripthandlerfactory, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>       <add name="scripthandlerfactoryappservices" verb="*" path="*_appservice.axd" precondition="integratedmode" type="system.web.script.services.scripthandlerfactory, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>       <add name="scriptresource" precondition="integratedmode" verb="get,head" path="scriptresource.axd" type="system.web.handlers.scriptresourcehandler, system.web.extensions, version=4.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>     </handlers>   </system.webserver>    <system.servicemodel>      <bindings>       <wshttpbinding>         <binding name="wshttpbinding_iservice" closetimeout="10:01:00" opentimeout="10:01:00" receivetimeout="10:10:00" sendtimeout="10:01:00"                  bypassproxyonlocal="false" transactionflow="false" hostnamecomparisonmode="strongwildcard" maxbufferpoolsize="2147483647"                  maxreceivedmessagesize="2147483647" messageencoding="text" textencoding="utf-8" usedefaultwebproxy="true" allowcookies="false">           <readerquotas maxdepth="2147483647" maxstringcontentlength="2147483647" maxarraylength="2147483647" maxbytesperread="2147483647" maxnametablecharcount="2147483647" />           <reliablesession ordered="true" inactivitytimeout="10:10:00" enabled="false" />         </binding>       </wshttpbinding>     </bindings>      <services>       <service name="selfhostedrestservice.service" behaviorconfiguration="selfhostedrestservice.restservicebehavior">         <endpoint address="selfhostedrestservice.service" binding="webhttpbinding" contract="selfhostedrestservice.iservice" behaviorconfiguration="web">           <identity>             <dns value="localhost"/>           </identity>         </endpoint>       </service>     </services>      <behaviors>       <servicebehaviors>         <behavior name="selfhostedrestservice.restservicebehavior">           <servicemetadata httpgetenabled="true"/>           <servicedebug includeexceptiondetailinfaults="false"/>         </behavior>       </servicebehaviors>       <endpointbehaviors>         <behavior name="web">           <webhttp/>         </behavior>       </endpointbehaviors>     </behaviors>    </system.servicemodel>  </configuration> 

here interface:

namespace contracts {     [servicecontract]     public interface iservice     {         [xmlserializerformat]         [operationcontract]         [webget]         xmldocument postgeneralxmldocument(stream strinput);          [operationcontract]         [webinvoke]         string postmessage(string inputmessage);     } } 

why throwing exception?

based on actual inner exception,

only 1 <configsections> element allowed per config file , if present must first child of root <configuration> element.

the cause of problem <startup>..</startup> section, if <configsections> present in web.config must first child of root element. moving <startup>..</startup> section after <configsections> resolve problem.

  1. uncomment <configsections> element.

  2. move <startup><supportedruntime version="v4.0" sku=".netframework,version=v4.0"/></startup> section below <configsections>...</configsections> section.


update:

for exception "the contract name 'selfhostedrestservice.iservice' not found in list of contracts implemented service 'service'."

please correct namespace used, mismatch in namespace used in interface & web.config.

in contract: namespace contracts

in web.config: selfhostedrestservice.iservice

either,

  • change namespace selfhostedrestservice in interface

or alternatively

  • use contracts.iservice in web.config.

update 2 exception "operation 'postgeneralxmldocument' in contract 'iservice' has query variable named 'strinput' of type 'system.io.stream', type 'system.io.stream' not convertible 'querystringconverter'. variables uritemplate query values must have types can converted 'querystringconverter'." seems using variable named 'strinput' of type 'system.io.stream', directly can't serialized. try serialize string & deserialize @ other end.

one useful link : http://www.techques.com/question/1-5241661/wcf-complex-json-input-error-(not-convertible-by-querystringconverter)

hope helps.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -