WCF - Access Denied when creating folder - wcf

I've built a WCF service and want to host it on IIS. One of the features I want is to programmatically create a subfolder in the WCF webhost root, to persist some json files.I've tested it on my local dev IIS Express and it works fine, which is no surprise 'cause I have all the necessary permissions.Now, I want to publish it on my public domain. For this, I've created a subdomain on Plesk and a subfolder within httpdocs. I've copied the web.config file and the Bin folder from my dev machine to this folder on my domain. But, when I enter the svc url on my browser I get a access denied exception (the folder is created in a dependency injected in my service, that's why I get the exception at this point).
Server Error in '/' Application.
Access to the path
'C:\Inetpub\vhosts\««my domain»»\httpdocs\VersioningService\Repository' is
denied.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access to the
path
'C:\Inetpub\vhosts\««my domain»»\httpdocs\VersioningService\Repository' is
denied.
Below is my web.config:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<customErrors mode="Off"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true">
<serviceActivations>
<add factory="VersioningService.Services.ManualMajorMinorVersioningHostFactory"
relativeAddress="~/ManualMajorMinorVersioning.svc"
service="VersioningService.Services.ManualMajorMinorVersioning" />
</serviceActivations>
</serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
This is what I use to get the current assembly path:
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Note: If I use the ProgramData folder I get NO exception, but I would prefer to have a subfolder within the WCF folder.
This is clearly a permissions issue. Do I need to add something else to my web.config?Do I need to ask my host provider to give some extra permissions to some user?

Related

WCF only accepts localhost

I'm writing my first WCF in Visual Studio Express, and configured it to run under IIS Express. My Web.config is as bellow. From my browser I can access the service if I do a Get request on http://localhost:50000/Service1.svc, but not http://10.0.0.26:50000/Service1.svc where 10.0.0.26 is my ip. How to configure WCF of IIS Express to accept IP addresses. Ultimatly my service is tio be reached accross the network.
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="medSaveWCF.Service1">
<endpoint address="../Service1.svc"
binding="webHttpBinding"
contract="medSaveWCF.IService1"
behaviorConfiguration="webBehaviour" />
</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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
</customHeaders>
</httpProtocol>
<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>
(Side note: I have blogged about this, including additional setup steps you need to use SSL: http://blog.kutulu.org/2015/01/using-iis-express-with-remote-systems.html)
The problem is that IIS Express only listens on the localhost address, by default. The reason is, IIS runs as a user process, but uses the same HTTPD.SYS system library that the full IIS does. By default the HTTPD.SYS configuration does not allow user processes to bind to an external address. To fix this you'll need to do three things:
Edit the IIS configuration to bind to a new port
Update HTTPD.SYS to permit your user to use that new binding.
Tell WCF you have multiple bindings.
Step One: IIS Express Setup
The IIS Express configuration is done directly through the XML configuration file, which is found at:
C:\Users\[username]\Documents\IISExpress\config\applicationhost.config
If your project is already set up to work with IIS Express, you'll find a configuration block starting around 150 lines into the file -- look for the XML <sites> tag, and you'll find a <site> element:
<site name="MySolution.MyProject" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/"
physicalPath="C:\Projects\MySolution\MyProject" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:50000:localhost" />
</bindings>
</site>
Inside that <bindings> element is the list of ports and hostnames that IIS Express binds to when running that particular site, you just need to add a new binding element:
<binding protocol="http" bindingInformation="*:50000:10.0.0.26" />
Step Two: HTTPD.SYS Permissions
Full disclosure: this step is optional if you are willing to run Visual Studio and IIS Express as an admin user. But that defeats the entire purpose of IIS Express, which is a user-mode web server, so don't do that.
Instead, you just need to use the netsh command to reconfigure HTTPD.SYS to allow you to bind to the ports you want. Specifically, you need to use the http add urlacl command.
Launch an administrative command prompt and/or PowerShell prompt and do this:
netsh http add urlacl url=http://10.0.0.26:5000 user=Everyone
Once both are done, shut down IIS Express so VS will restart it, and you should be all set.
I wrote myself a small Powershell script to go through and do this for a whole range of ports:
$LowPort = 50000
$RangeSize = 99
for ( $i = 0; $i -le $RangeSize; $i++ )
{
netsh http delete urlacl url="http://${IISHost}:$($LowPort + $i)/"
netsh http add urlacl url="http://${IISHost}:$($LowPort + $i)/" user=Everyone
}
That way I don't have to remember to do this every time, I just need to use a port in the 50000 - 50100 range.
Step Three: Inform WCF
By default, WCF only binds to one site per project. For real IIS this is fine, because that's the *:80 binding you probably want. For IIS Express, you need separate bindings per IP address so you need to tell WCF to use them all. This is easy, just add this to your WCF configuration:
<configuration>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
Once all that's done, shut down IIS Express and let VS restart it and you should be all set.

WCF Service IIS Hosting Error

i have a couple of web site in IIS now i have added a WCF service under sites>default website > MyWCFService.
When i tried to browse the Service1.svc file through content view in IIS i was presented below error
HTTP Error 404.17 - Not Found
The requested content appears to be script and will not be served by the static file handler.
Detailed Error Information
Module: StaticFileModule
Notification : ExecuteRequestHandler
Handler : StaticFile
Error Code 0x80070032
Requested URL : https://localhost:443/MyWCFService/Service1.svc
Physical Path : D:\Inetpub\wwwroot\MyWCFService\Service1.svc
Logon Method : Negotiate
Logon User : XXXXXXXXXXXXXX
Most likely causes:
•The request matched a wildcard mime map. The request is mapped to the static file handler. If there were different pre-conditions, the request will map to a different handler.
Things you can try:
•If you want to serve this content as a static file, add an explicit MIME map.
Here is my webconfig file:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="MyService.Service1" behaviorConfiguration="MyService.Service1Behavior">
<endpoint address="Service1" binding="basicHttpBinding" contract="MyService.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyService.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
The service runs fine in my local machine, but it fails in IIS, may be something to do with IIS Settings?
I'm running on IIS version 7.5.7600.
Thanks in advance
Edit 1: : Installed WCF Activation (HTTP Activation and Non-HTTP Activation) under .net Framework 3.5.1 features in via Server mnager. Now the error when i tried browing the service1.svc file through browser is
Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
i have added this entry in web.config file as well
<compilation debug="true" strict="false" explicit="true">
<assemblies>
<add assembly="System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
No great change in output.
The requested content appears to be script and will not be served by the static file handler.
Don't use Content View, use a browser.
See also Script not served by static file handler on IIS7.5.

WCF service calls works properly in Intranet, but not in internet

I've been working with silverlight application for over a month now, and have stumbled upon a weird issue.
I have a WCF service running in IIS, accessible from the URL :
https://xyztestname.com/service1.svc
I am able to consume this in visual studio and when deployed from visual studio, am able to get proper call backs from WCF service and everything works fine.
When i deploy the package files in to the same folder as the Service1.svc in IIS, the WCF service is not hitting properly.
Please help me resolve this issue :(! Here is my web.config file.
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpointBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="InformationService.Service1">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding"
name="BasicHttpEndpoint" contract="InformationService.IService1">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
I dont know where i am going wrong. But the same Virtual folder when accessed through intranet works fine, but doesn't work when accessed through internet :( Please help.
Edit:
After checking into the client config, i found out that Visual studio automatically resolved the URL into an intranet URL. When i changed back to the internet URL, i am getting an exception thrown.
n error occurred while trying to make a request to URI 'https://xyztestname.com/Service1.svc'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.
However, I have copied both the crossdomain and clientaccesspolicy files into the root of the application in IIS. :( Please help.
You have to deploy your application to the specific ip and port to be able to use it in internet.
I think you can.
To do this you need to edit applicationhost.config file manually (edit bindingInformation '::')
To start iisexpress, you need administrator privileges
1 – Bind your application to your public IP address
Normally when you run an application in IIS Express, it’s only accessible on http://localhost:[someport]. In order to access it from another machine, it needs to be bound to your public IP address as well. Open D:\Users[YourName]\Documents\IISExpress\config\applicationhost.config and find your site. You will find something like this:
<site name="YOUR PROJECT NAME HERE" id="2">
<application path="/">
<virtualDirectory path="/" physicalPath="YOUR PHYSICAL PATH HERE"
<binding protocol="http" bindingInformation="*:58938:localhost" />
</bindings>
</site>
In , add another row:
<binding protocol="http" bindingInformation="*:58938:192.168.1.42" />
(But with your IP, and port number, of course)
2 - Allow incoming connections
If you’re running Windows 7, pretty much all incoming connections are locked down, so you need to specifically allow incoming connections to your application. First, start an administrative command prompt. Second, run these commands, replacing 192.168.1.42:58938 with whatever IP and port you are using:
netsh http add urlacl url=http://192.168.1.42:58938/ user=everyone
This just tells http.sys that it’s ok to talk to this url.
netsh advfirewall firewall add rule name="IISExpressWeb" dir=in protocol=tcp localport=58938 profile=private remoteip=localsubnet action=allow
This adds a rule in the Windows Firewall, allowing incoming connections to port 58938 for computers on your local subnet.
And there you go, you can now press Ctrl-F5 in Visual Studio, and browse you site from another computer!

Can't define the endpoint after creating my WCF

I wanna learn WCF so I decided to try out creating some application that uses it. Well what I have in mind is I have 2 databases and I wanna create also an SSIS custom data source extension that calls the web service and passes the data (from one database) to an SSIS ADO.Net Data source (into the second database).
Now I created the 2 databases using SQL Server with one table in each. Then I Added a Connection in Visual Studio and then specified the server instance. (didn't create any .mdf files). I added this connectionString in the Web.Config file
<connectionStrings>
<add name="dbconnection" connectionString="
Data Source = SARE-VAIO;
Integrated Security = true;
Initial Catalog = Database1"/>
</connectionStrings>
When I wrote my service which basically populates Database1 with data, I wanted to define the endpoints but when I clicked on the "Edit WCF Configuration" it says 'No Service' is defined? What possibly am I doing wrong here? I want to create an error free service to be able to use it as a source in the SSIS package.
PS. My service has a basicHttpsBinding
UPDATE: I'm using VS 2012 with .Net Framework 4.5
UPDATE 2:
I skipped the endpoint definition for now and went ahead with testing and deploying my WCF. When I invoke the service it says the following error
Failed to invoke the service. Possible causes: The service is offline or inaccessible;
the client-side configuration does not match the proxy; the existing proxy is invalid.
Refer to the stack trace for more detail. You can try to recover by starting a new proxy,
restoring to default configuration, or refreshing the service.
Here is my web.config file
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="dbconnection" connectionString="Data Source = SARE-VAIO; Integrated Security = true; Initial Catalog = Database1"/>
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<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>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
To answer your questions
(1) Why WCF configuration editor shows 'No service is defined' error : Your web.config does not have any services and endpoints defined explicitly (Note: When you host this in IIS you will still get some endpoints added due to the defaults endpoint feature. But config editor tool shows only explicitly defined endpoints). That's the reason the config editor tool shows this message. But you can use the tool to add services and endpoints.
(2) After deploying the service see if the service successfully activated. You can do this by browsing to the metadata URL (your config has metadata enabled). Make sure your service WSDL help page and WSDL shows up fine. If not fix that issue first.
(3) If you are looking a default https endpoint after hosting it in IIS, make sure your IIS has https binding configured with an SSL certificate.
Hope this helps!
Thanks!

Error on <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> while publish on IIS

I have one test wcf service with default methods and web config is :
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
on local IIS it works fine when i publish it.But when i try to publish on remote IIS i am getting this error :
Description: An error occurred during the processing of a
configuration file required to service this request. Please review the
specific error details below and modify your configuration file
appropriately.
Parser Error Message: It is an error to use a section registered as
allowDefinition='MachineToApplication' beyond application level. This
error can be caused by a virtual directory not being configured as an
application in IIS.
Line 23: </service>
Line 24: </services>
**Line 25: <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>**
Line 26: </system.serviceModel>
Line 27: <system.webServer>
thanks in advance.
I got the solution :
In IIS if the application is indeed an application, not a virtual directory? The icon should be something looks like the earth, not a folder's icon. If not, please convert it to an application right click on the folder select deploy and then Application.Service running with
Make sure you browse to the actual web folder (that contains Bin, *.svc and web.config) when creating web application, not the parent folder.
If you working framework 4.0 only one ENDpoint so write multipleSiteBindingsEnabled="False"
serviceHostingEnvironment multipleSiteBindingsEnabled="False"
Using this it will be run