Get folder on server from WCF - wcf

On the first code, on the localhost I get the Bin folder - which I expect. On the server I get an error. These are all the methods I have tried.
AppDomain.CurrentDomain.BaseDirectory
Hosting.HostingEnvironment.ApplicationPhysicalPath
System.Web.HttpContext.Current.Server.MapPath("~") 'for the root
Can anyone enlighten me on how I can get the root directory on the server from WCF service?
Error:

According to the accepted answer in this question you should use
string path = HostingEnvironment.MapPath("~");

add [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
to class and try

Related

How to get the virtual path to where WCF is hosted on IIS rather than the physical path?

I have a WCF Service hosted on IIS server, however i try to get the path to which the service is hosted on the IIS, that is similar to: 192.168.1.xx/WCFHosted/VirtualDirectory, by using the below code:
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath
The above returns a path to the Physical path corresponding to where the IIS Hosted WCF is originally linked from, that is Ex: D/MyService/WCFHosted,
How can i return the IIS Hosted path for the WCF,
Please Advise,
Thanks in Advance
Try the below code snippets.
System.Environment.MachineName+"/" + System.Web.Hosting.HostingEnvironment.SiteName +System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
We can convert the Machine name to an IP address by using DNS.GetHostAddress(hostname) method.
var result = Dns.GetHostAddresses(Environment.MachineName).ToList();
foreach (var item in result)
{
Console.WriteLine(item);
}
Feel free to let me know if there is anything I can help with.

IIS looking at wrong path for MVC configuration file

When I run MVC4 my application using Visual Studio Development Server, my application is able to load all configuration files correctly. But when I try to run it under the Local IIS Web server, it throws this error
{"Could not find a part of the path 'C:\\Windows\\SysWOW64\\inetsrv\\~\\nhibernate.config'."}
It's looking at the wrong folder. The config file is directly in the root of my web project. Why is this happening. How can I fix it. I'm pretty sure that nhibernate looks for this file in order to load it's properties. Unless you know how to move nhibernate configuration files into the web.config file, please don't recommend that, I get a "Unrecognized configuration section hibernate-configuration" when ever I copy it over
Edit:
by the way, I'm pretty sure it's looking at the following key in appSettings my app settings section
<add key="nhibernate.config" value="~/nhibernate.config" />
Edit2:
here is the stack trace that goes into how Nhibernate is getting this value, and trying to find the configuration file
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting)
at NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader)
at NHibernate.Cfg.Configuration.Configure(XmlReader textReader)
at NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig)
at NHibernate.Cfg.Configuration.Configure(String fileName)
at AndroMDA.NHibernateSupport.DefaultSessionManager.BuildSessionFactory()
So it wasn't actually NHibernate that was using this file. My boss decided to use something called AndroMDA to generate code, and part of that generation was some NHibernate Support that I couldn't see into.
If anybody is interested, I created a new implementation of ISessionManager, which looks exactly like DefaultSessionManager except for the following member function and variable.
public class ServerMapSessionManager : ISessionManager
{
//other interface implementation...//
public static HttpServerUtility Server { get; set; }
public String TranslateConfigPath(String virtualPath)
{
return Server.MapPath("/"+virtualPath);
}
}
Then in App_start I call the following
SessionManagerFactory.SessionManager = new MVCFramework.Core.Common.ServerMapSessionManager();
MVCFramework.Core.Common.ServerMapSessionManager.Server = Server;

IIS Hosted WCF accessing file

In development environment
string AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path = AppPath + "\\options.xml";
Works.
When Hosted by production IIS7 server I put Options.xml in the bin environment but the path produced by the above code is a asp.net temp directory.
So what is the correct technique to access the file in both development and production environment?
thanks
Is this information you are using to support your service?
If so, is there any reason you couldn't just make a custom config class and read it as a section from your web.config?
You can use this:
string path = System.IO.Directory.GetCurrentDirectory();

SVC client proxy and WSDL hosted on remote server

I have one wsdl and xsd then I generated a SVC client proxy from a wsdl, wsdl is hosted on windows server 2003 (outside local domain), somewhere on internet. XSD is also located at same place. I can see them in web browser, no issue there.
Then I made a simple console application client, when I run the client, I see in fiddler that correct soap response is there but in console application response is null, i.e. SVC client can not deserialize it.
I am sure that xsd + wsdl are correct and response too(I see in fiddler).
When I put wsdl and xsd's on local domain server(intranet), it's all good, I see response in application properly deserialized.
If I compare both situation in fiddler, I don't see any difference in raw response.
Few more points: I am not hosting WCF service in IIS. I have got a different service running remotely. Let me know if I clarify more things.
Thank you.
Edit/ Update:Cleaned post. Problem seems not to be IIS6 or IIS7, it's remote server creating trouble with schema location. In SVC proxy client MessagContract namespace if pointed to a outside server, problem occurs else on intranet server it runs just fine.
Example:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class responseTypeOfMethod1
{
[System.ServiceModel.MessageBodyMemberAttribute( Namespace = "http://testserver.xxxxx/yyyy.wsdl", Order = 0)]
public RAT_type RAT;
................
....
..
}
if http://testserver.xxxxx points to local server, all fine, if points to an internet server , response is null
There are 2 things that could be happening here:
The response is not being returned from the remote server
The client is not able to deserialize the response
Use wireshark or some other network sniffer to see what response in coming back.
If the problem is due to inability to deserialize, it is probably a difference in the namespace.
Problem solved. It was namespace problem of-course, took a long time to find out a nasty place in server code, a tool we use internally. Thank you all who replied for your time.

Zero dependency from the client configuration files for WCF services

Our aim is to have Zero dependency from the client configuration files for WCF services.
we are using ConfigurationChannelFactory() to create the channel and specify the ConfigSettings.
ConfigSettings is loaded using the following code
ConfigurationManager.OpenExeConfiguration(ConfigFilePath);
So we have to provide the ConfigFilePath here.
we have both windows and web clients.
we have used below approaches to find out the path
AppDomain.CurrentDomain.BaseDirectory + "bin\\" + executingAssembly.GetName().Name + ".dll"
Web client : AppDomain.CurrentDomain.BaseDirectory gives root folder of the
web applicaton so its works fine
Windows client : AppDomain.CurrentDomain.BaseDirectory gives path upto Debug/Release folder
so, its throws error
Assembly.GetExecutingAssembly().Location
Web client : Assembly.GetExecutingAssembly().Location gives path to the ASP.Net temp.
files , where we dont have the config files. So its throws an error.
Windows client : Assembly.GetExecutingAssembly().Location gives the correct location,
so its works fine.
In order to make it works in web clients, we have to add the following key in client's web.config files.
<hostingenvironment shadowcopybinassemblies="false">
but it adds a dependency to the clients.
Can you guys please help me to find the path without worrying about the clients?
have you tried this? I used GetCallAssembly() instead of GetExecutingAssembly() as this lives in a utility class in our project.
public static string AssemblyDirectory
{
get{
string codeBase = assembly.GetCallingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Could you just probe both paths? In other words, check the Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin") folder and if you can't find the config file in there, check the Assembly.GetExecutingAssembly().Location folder instead? Since you indicate that the first approach works for web but not Windows clients, while the second approach works for Windows clients but not web, go with both!