WCF service on local IIS not working

Hi,

I have made a WCF service which I consume in an WPF application. I all works fine when I run the service in the VS development server but when I install it in the local IIS I run into problems - se the attached screen dump. Sorry about the Swedish but it says something like this:

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

If I go to http://localhost/Service1.svc in a web browser it looks fine.

Can you help with this?

Best regards
RTSOL
WCFError.png
RTSolAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RTSolAuthor Commented:
Hi again,

I should add that I am using VS 2012 and Windows 10.

-RTSOL
systechadminConsultantCommented:
it seems that exceptions are not enable. please refer below link which might help you to understand the isseu

http://social.technet.microsoft.com/wiki/contents/articles/17418.the-famous-system-servicemodel-faultexception1-was-unhandled-by-user-code.aspx
RTSolAuthor Commented:
Hi,

I have tested some more and it seems that methods that involve calls to an SQL server is the ones that doesn't work. Is there a cross domain problem maybe. As I said earlier it works fine with the development server but not in my local ISS. Here is my service:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Security;

namespace WcfTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string SDConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbSmartDocConnectionString"].ConnectionString;
        public string sql = string.Empty;

        public string getCountry() //Works fine in the development server but not in ISS
        {
            string name = string.Empty;
            using (SqlConnection spConn = new SqlConnection())
            {
                sql = "SELECT TOP 1 Land FROM Countries";
                spConn.ConnectionString = SDConnectionString;
                SqlCommand cmd = new SqlCommand(sql, spConn);
                spConn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    name = rdr[0].ToString();
                }
            }
            return name;
        }

        public Boolean Login(string usr, string psw) //Works fine in the development server but not in ISS
        {
            Boolean ok = false;
            if (Membership.ValidateUser(usr, psw))
            {
                ok = true;
            }
            return ok;
        }

        public string GetData(int value) //Works fine in the development server as well as in ISS
        {
            return string.Format("You entered: {0}", value);
        }

    }
}

Open in new window

Hope you can help!

-RTSOL
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

RTSolAuthor Commented:
Hi,

I have now installed the service on a remote server (2008 R2) and there it works fine from mu WPF app.

So, it works on my development server, on the remote server but not on my local IIS.

Can someone please help me?

Best regards
Robert Tell
MlandaTCommented:
This sounds like something related to permissions and authentication issues in your communications with the databases. Do you connection strings use Windows or SQL authentication?.

To simplify debugging, catch exceptions and log them to the Debug window.  
public string getCountry() //Works fine in the development server but not in ISS
{
    string name = string.Empty;
	try
	{
	    using (SqlConnection spConn = new SqlConnection())
	    {
	        sql = "SELECT TOP 1 Land FROM Countries";
	        spConn.ConnectionString = SDConnectionString;
	        SqlCommand cmd = new SqlCommand(sql, spConn);
	        spConn.Open();
	        SqlDataReader rdr = cmd.ExecuteReader();
	        while (rdr.Read())
	        {
	            name = rdr[0].ToString();
	        }
	    }
	} catch (Exception ex) 
	{
		Debug.WriteLine(ex.ToString());
	}
	
    return name;
}

public Boolean Login(string usr, string psw) //Works fine in the development server but not in ISS
{
    Boolean ok = false;
	try
	{
	    if (Membership.ValidateUser(usr, psw))
	    {
	        ok = true;
	    }
	} catch (Exception ex) 
	{
		Debug.WriteLine(ex.ToString());
	}
    return ok;
}

Open in new window

Since you are hosting on your computer, you need to step into the code and see the exception details on the WCF side. You could also just use the F10/F11 keys as usual.

ASIDE:
A very important thing that seems to be missing from your code is try..catch statements. These help you to deal with the exceptions that happen in your code. A good logger for .NET is log4net (http://www.beefycode.com/post/log4net-tutorial-pt-1-getting-started.aspx)

1 - It's easy to add to your solution (via Nuget)
2 - It will log a lot of details about the exception
3 - It can write to a text file OR send the logs to a UDP port
4 - You can use a tool like Log2Console to vierw the logs and see what is happening internally in the system in real time (https://log2console.codeplex.com/)
5 - Log2Console gives you a really nice view into your application with excellent filters and stuff.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
are you using a sql login (you normally must) and is your connection string correct for the sql server you want to use (local machine)
RTSolAuthor Commented:
Hi,

Thanks for the input. I caught the error in the service and wrote the error message to a text file. It says:

Cannot open database "dbSmartDocBoSmart" requested by the login. The login failed.
Login failed for user 'NT instans\SYSTEM'.

So I changed to sql login and it works. This raises this question:

In my production server I use windows login and it works provided I set the identity in the application pool to LocalSystem. So - why doesn't this work in my local IIS. I did set the the identity in the application pool to LocalSystem also here?

Can you please comment on this and I will close the question.

Best regards
Robert Tell
MlandaTCommented:
LocalSystem in either case refers to the local machine itself. This is technically a different account between your computer and the server.

Also, ( https://msdn.microsoft.com/en-us/library/ms677973(v=vs.85).aspx ):
When a service runs under the LocalSystem account on a computer that is a domain member, the service has whatever network access is granted to the computer account, or to any groups of which the computer account is a member.
 Your laptop and the server probably have different permissions in the AD. So one might allow access to the database, whilst the others might not.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.