When running the WCF test client and debugging the WCF service locally, everything works fine. After I deploy it to IIS on a remote server and then try loading it with the WCF test client I start getting "Object not set to an instance of object" errors. I added a try-catch to the service and the error changed to "Method not allowed".
I have been reading up on these errors all day and most posts relate to people using JSON or AJAX to access their WCF client. I have tried many of the suggested solutions, most of which were changes to web.config. I am simply accessing it using the test client at this stage, it will be consumed by a Winform application once proven to work.
The 2 methods exposed on the service both create connections to a SQL server on a different domain, although there is a trust set up. I am thinking this could be part of the problem.
My Service Interface
[ServiceContract]
public interface IMeterQueryService
{
[OperationContract]
List<Meter> FindMeter(string mprn);
Implemented Service
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MeterQueryService : IMeterQueryService
{
public List<Meter> FindMeter(string mprn)
{
using (var da = new DataAccess())
{
var meters = da.GetMeter(mprn);
da.Dispose();
return meters;
}
}
App Config File
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<connectionStrings>
<add name="Tims" connectionString="hidden" providerName="System.Data.SqlClient"/>
</connectionStrings>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="TimsAPI.MeterQueryService">
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="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="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
web config file, site references the WCF service library.
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="TimsAPI.MeterQueryService">
<endpoint address="http://easvr33:1000/" binding="basicHttpContextBinding" bindingConfiguration=""
contract="TimsAPI.IMeterQueryService" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="localhost" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
</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>
Update
I recreated the WCF service as a WCF Service website, rather than a library and it all works now. Something weird between the 2 config files I think. I would still be interested to know why it never worked but I do also understand it may be hard to diagnose for someone without full access to the source
When a WCF service is hosted via an IIS website the service app.config is not used and instead the hosing site's web.config is loaded. All settings needed for the service to work must be duplicated to the web.config of the hosting site.
Might be an issue with serving the SVC to begin with. From here:
1.Open your Virtual Directory’s Properties.
2.Go to the Directory tab.
3.Click Configuration.
4.Click Add…
5.Provide the following information:
a.Executable: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
b.Extension: .svc
c.Verbs: GET,HEAD,POST,DEBUG
Related
I'm new to using web services.
I created a web service in C# using visual studio 2017 (the service is a .svc file).
This web service is published to a folder on a remote machine.
When I connect to the remote machine, I can run the web service with the url:
http://localhost:1869/ServiceName.svc/
But when I'm trying to run the web service from my computer, I tried to modify the url by replacing 'localhost' with the ip address but it doesn't work.
Is it possible to access remotely a local web service?
If not, what is the best way to publish the web service so that it can be accessed remotely?
Thanks for your help!
-EDIT-
See Web.config code below.
I tried to create a web server on the remote machine and place the Visual Studio solution project / compile it in C:\inetpub\wwwroot, did not help
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="RsConnString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|RestDB.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>
Thanks for your help!
I was able to make it work following instruction from there: link
It boils down to configuring IIS and the website correctly.
The key questions to answer were:
Have you enabled basic authentication at the server level in IIS?
Have you enabled remote connections to the Web Management Service?
Have you started the Web Management Service?
Are there management service delegation rules in place?
Does your firewall allow incoming connections to the server on TCP port 8172?
I have created a WCF service and am having some trouble testing it once it has been deployed. Here is the PowerShell I am using to test it:
$service = New-WebServiceProxy -Uri http://localhost:16651/Service.svc
$service.GetList()
When debugging the service from Visual Studio with F5, I can call this script without any issue. GetList() returns a long list of telephone numbers.
However, when I host the site on IIS and run the above script, I get an empty return value.
Service Factory
So following this question, I added this attribute to Service.svc:
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
However, this resulted in my script returning an error on the first line:
New-WebServiceProxy : Object reference not set to an instance of an object.
Which does not make any sense to me, as I am not referencing any empty objects... (this error appears when debugging and when hosting over IIS).
Web.Config
Next, I tried updated my web.config as per the linked question:
<services>
<service name="LyncWebService.Service">
<endpoint binding="webHttpBinding" contract="LyncWebService.IService" behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
However, now when I try to run my PowerShell script I get this error both during debugging and when hosting on IIS (again on the first line):
The HTML document does not contain Web service discovery information.
I am totally lost here and have no idea what is going wrong. I suspect it is to do with my config file, as it did seem to work when debugging from VS before I messed with the configuration.
Any help or guidance is much appreciated - and please let me know if I can provide any other information or test anything.
Here is the code that makes up my service currently:
Service.svc.cs
namespace LyncWebService
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke]
List<string> GetList();
}
public class Service : IService
{
public List<string> GetList()
{
return Ps.GetAssignedNumbers(#"
Set-ExecutionPolicy RemoteSigned
Import-Module Lync
$(Get-CSUser).LineUri"
);
}
}
}
Web.Config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
</system.web>
<system.serviceModel>
<services>
<service name="LyncWebService.Service">
<endpoint binding="webHttpBinding" contract="LyncWebService.IService" behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<!--<add binding="basicHttpsBinding" scheme="https"/>-->
<add binding="webHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" 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>
Thanks to Jonathan Coffey, I realised that the service was being run by the LocalSystem account.
After changing this to my own user account and hosting the original web.config on IIS, I am now able to retrieve the full list using my PowerShell script.
Open IIS
Application Pools
Right-Click the Application pool
Advanced Settings...
Process Model -> Identity
Custom Account (Don't forget to include the domain for the User Name!)
None of the members of my team has ever been able to get a particular WCF service in our solution to work on our local machines. We've inherited it in legacy code and are trying to replace it, but it's very difficult to tell what it's doing since we can't run the debugger on it and we can't even get a response from it while debugging the main site that uses it.
The main part of our application is a web site. This particular service is hosted in a separate application pool on IIS due to some problems with using Excel interops (which this service uses) in the same app pool as the main site.
The service appears to use net.tcp for the protocol, and I have enabled the Windows Communication Foundation Non-HTTP Activation feature on my machine. I have also enabled the protocol on the Default Website and the node underneath it which is the WCF service in question (is this redundant?).
I can attach the debugger to w3wp.exe processes for both the site and the service. When the site makes the call to the service, however, an error is immediately returned and no breakpoints in the service are hit. The error reads:
The service 'MySvc.svc' cannot be activated due to an exception during
compilation. The exception message is: The type 'MyNamespace.MySvc',
provided as the Service attribute value in the ServiceHost directive,
or provided in the configuration element
Note, I have obviously redacted the real service name, etc, from this post. After attempting to follow solutions proposed on numerous similar questions, I have gotten nowhere. I am wondering if the problem is exacerbated by the separate app pools.
Below is the Web.config from the service project. The SVC file is named UploadAndImport.svc.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<!-- REDACTED SECTIONS PERTAINING TO LOGGING AND ENTERPRISE LIBRARY -->
</configSections>
<!-- REDACTED SECTIONS PERTAINING TO LOGGING AND ENTERPRISE LIBRARY -->
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.6.1" />
<identity impersonate="true" />
<pages controlRenderingCompatibilityVersion="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="ProjectName.UploadAndImport">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="ExcelServiceEndPointHTTP" contract="ProjectName.IUploadAndImport" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint binding="netTcpBinding" bindingConfiguration="" name="ExcelServiceEndPointTCP" contract="ProjectName.IUploadAndImport" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!-- This line ignores the error that 'An ASP.NET setting has been detected that does not apply in Integratd managed pipeline mode (system.web/identity#impersonate is set to true)'-->
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
</configuration>
The directive in the SVC file looks like this:
<%# ServiceHost Language="VB" Debug="true" Service="ProjectName.UploadAndImport" CodeBehind="UploadAndImport.svc.vb" %>
I have imported an a wcf service and it is accessible from local host but when I use my public ip address i am unable to reach it.
This is the contents of my web.config file.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"
targetFramework="4.0" />
</system.web>
<system.serviceModel>
<extensions>
<bindingExtensions>
<add name="clearUsernameBinding"
type="WebServices20.BindingExtenions.ClearUsernameCollectionElement, ClearUsernameBinding" />
</bindingExtensions>
<!-- Add the inspector attribute as a behavior for displaying SOAP XML packets -->
<behaviorExtensions>
<add name="consoleOutputBehavior"
type="WcfService1.ConsoleOutputBehaviorExtensionElement, WcfService1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<bindings>
<clearUsernameBinding>
<binding name="myClearUsernameBinding"
messageVersion="Soap12" />
</clearUsernameBinding>
</bindings>
<behaviors>
<!-- Add the inspector behavior -->
<endpointBehaviors>
<behavior name="inspectorBehavior">
<consoleOutputBehavior />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="HelloWorldServiceBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WcfService1.CustomUserNameValidator, WcfService1" />
</serviceCredentials>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="HelloWorldServiceBehavior"
name="WcfService1.HelloWorldService">
<endpoint address=""
binding="clearUsernameBinding"
bindingConfiguration="myClearUsernameBinding"
contract="WcfService1.IHelloWorldService" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
I have a console app that is able to communicate with it but ONLY with localhost.
This is how I am able to get to it. It is running on port 14946 and have port forwarded that port to the computer that has the service running. It is hosted on IIS.
http://localhost:14946/HelloWorldService.svc?singleWsd
If I use : {publicIP}:14946/HelloWorldService.svc?singleWsd
I get a 500 internal error which means its a configuration issue but I can't seem to pinpoint what the problem is.
EDIT: I am now receiving this error.
The type 'WcfService1.HelloWorldService', provided as the Service attribute value in the ServiceHost directive could not be found. I am able to get to it over the web now but receiving this error now. In order to fix the previous error I had to: 1.) Go to add remove features 2.) Expand MS .NET Framework 3.) Flip on both HTTP and Non-HTTP activation for WCF
Thanks,
If you properly hosted it in IIS you can just remove the port in endpoint and access it directly, something like this: ( add virtual application if necessary )
http://<public ip>/HelloWorldService.svc?singleWsd
It is working in your localhost because it was hosted in IIS Express upon debug mode and automatically assigned that port.
We already created an webservice in Visual Studio 2013 with .Net 4.5. This webservice runs well local in the same computer.
Right now we want to export this webservice to an Windows Server 2008 with IIS. We already made an webservice running in port 8080.
But when we copy the exported files to the root directory of this webserver the folowwing error occured:
Error Summary
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Detailed Error Information
Module
IIS Web Core
Notification
Unknown
Handler
Not yet determined
Error Code
0x80070032
Config Error
The configuration section 'system.serviceModel' cannot be read because it is missing a section declaration
Config File
\\?\C:\inetpub\wwwroot\YOR24Websevices\web.config
Requested URL
http://localhost:8080/
Physical Path
Logon Method
Not yet determined
Logon User
Not yet determined
Config Source 10: </system.web>
11: <system.serviceModel>
12: <bindings>
The webconfig we exported in Visual studio is:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="RightNowSyncBinding">
<security defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport"
requireDerivedKeys="true" securityHeaderLayout="Lax" includeTimestamp="false">
<localClientSettings detectReplays="false" />
<localServiceSettings detectReplays="false" />
</security>
<textMessageEncoding messageVersion="Soap12" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://tkbc-fleetsupport--tst.custhelp.com/cgi-bin/tkbc-fleetsupport--tst.cfg/services/soap"
binding="customBinding" bindingConfiguration="RightNowSyncBinding"
contract="RightNowServiceReference.RightNowSyncPort" name="RightNowSyncPort" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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="Yor24Service.Service">
<endpoint address="" contract="Yor24Service.IService" binding="basicHttpBinding"/>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
</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>
What is the problem with this generated config file?
Starting with .Net Framework 4 service behaviors do not require a name (MSDN). Before .Net 4 it was mandatory. Because your service behavior does not have a name:
<serviceBehaviors>
<behavior>
<!-- name attribute missing in behavior -->
</behavior>
</serviceBehaviors>
and IIS is throwing an error I suspect your app pool is not running .Net 4 or newer.
http://blogs.msdn.com/b/wenlong/archive/2010/11/23/why-does-machine-config-contain-invalid-xml-content-after-installing-net-3-5-patches.aspx
Try this link, solution 3 work with me
Solution 3: Run attached javascript file
The above solutions do not fix the Issue 3. So I created a javascript FixServiceModel30Reg.js to fix the problem. You can run it as following to fix all of the above issues. Steps:
· Download the script FixServiceModel30Reg.txt and save it to the root of c: drive.
· Rename it to FixServiceModel30Reg.js.
· Open the Command Prompt window and run the following command:
Cscript.exe c:\FixServiceModel30Reg.js