C# IIS 8中的WCF和EntityFramework错误服务器在处理请求时遇到错误

C# IIS 8中的WCF和EntityFramework错误服务器在处理请求时遇到错误,c#,asp.net,wcf,entity-framework,iis,C#,Asp.net,Wcf,Entity Framework,Iis,我是WCF新手,我开发了一个简单的WCF服务应用程序,其中添加了entityframework数据模型。 我有一个从数据库返回表的一个字段的方法。 我正在windows 8上开发visual studio 2013和sql server 2012。 当我在IISExpress中运行它时,一切正常。但是当我从iis运行它时,它显示以下错误: Request Error The server encountered an error processing the request. The excep

我是WCF新手,我开发了一个简单的WCF服务应用程序,其中添加了entityframework数据模型。 我有一个从数据库返回表的一个字段的方法。 我正在windows 8上开发visual studio 2013和sql server 2012。 当我在IISExpress中运行它时,一切正常。但是当我从iis运行它时,它显示以下错误:

Request Error
The server encountered an error processing the request. The exception message is
 'The  underlying provider failed on Open.'. See server logs for more details. The exception stack trace is:

at System.Data.Entity.Core.EntityClient.EntityConnection.Open() at
System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection() at   
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, 
IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean  
releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery`1.
<>c__DisplayClassb.<GetResults>b__9() at 
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 
operation) at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 
forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery`1.
<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at 
System.Lazy`1.CreateValue() at System.Lazy`1.LazyInitValue() at 
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() at 
WcfRestService.Service1.SelectRandomQuestion() at SyncInvokeSelectRandomQuestion(Object 
, Object[] , Object[] ) at 
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] 
inputs, Object[]& outputs) at 
 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 
 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& 
rpc) at 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& 
rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean 
isOperationContextSet)
请求错误
服务器在处理请求时遇到错误。异常消息是
“基础提供程序在打开时失败。”。有关详细信息,请参阅服务器日志。异常堆栈跟踪为:
位于System.Data.Entity.Core.EntityClient.EntityConnection.Open()处
System.Data.Entity.Core.Objects.ObjectContext.EnsureReconnection()位于
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 Func,
IDbExecutionStrategy executionStrategy,布尔型startLocalTransaction,布尔型
在System.Data.Entity.Core.Objects.ObjectQuery`1中释放ConnectionOnSuccess)。
c__DisplayClassb.b__9()位于
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1
操作)位于System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(可为null`1
forMergeOption),位于System.Data.Entity.Core.Objects.ObjectQuery`1。
b__0()在
System.Lazy`1.CreateValue()位于System.Lazy`1.LazyInitValue()位于
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()位于
WcfRestService.Service1.SelectRandomQuestion()位于SyncInvokeSelectRandomQuestion(对象
,对象[],对象[])位于
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象[])
输入、对象[]和输出)
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)
在System.ServiceModel.Dispatcher.ImmutableDispatcheRuntime.ProcessMessage5(MessageRpc&
rpc)在
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&
rpc)位于System.ServiceModel.Dispatcher.MessageRpc.Process(布尔值
等操作上下文集)
这是我的Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Data.Services.Providers;
using System.Text;

namespace WcfRestService
{
// 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.
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)] 

public class Service1 : EntityFrameworkDataService<CollaborationDBEntities>, IService1
{

    public string SelectRandomQuestion()
    {
        using (CollaborationDBEntities db = new CollaborationDBEntities())
        {
            var q = from questions in db.QuestionTable
                    select questions.QText;
            List<string> list = new List<string>();
            foreach (var item in q)
            {
                list.Add(item);
            }
            Random random = new Random();
            int selectedIndex = random.Next(0, list.Count);

            return list[selectedIndex];
        }
    }
    public string AddNewUser(string userName, string fName, string lName, string email, string imei)
    {
        using (CollaborationDBEntities db = new CollaborationDBEntities())
        {
            UserTable userObject = new UserTable() { UserName = userName, UserFName = fName, UserLName = lName, UserNumber = imei };
            db.UserTable.Add(userObject);
        }

        return null;
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.Data.Services.Providers;
使用系统文本;
命名空间WcfRestService
{
//注意:您可以使用“重构”菜单上的“重命名”命令来同时更改代码、svc和配置文件中的类名“Service1”。
//注意:要启动WCF测试客户端以测试此服务,请在解决方案资源管理器中选择Service1.svc或Service1.svc.cs并开始调试。
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults=true)]
公共类服务1:EntityFrameworkDataService,IService1
{
公共字符串SelectRandomQuestion()
{
使用(CollaborationDBEntities db=new CollaborationDBEntities())
{
var q=来自db.QuestionTable中的问题
选择questions.QText;
列表=新列表();
foreach(q中的var项目)
{
列表。添加(项目);
}
随机=新随机();
int selectedIndex=random.Next(0,list.Count);
返回列表[selectedIndex];
}
}
公共字符串AddNewUser(字符串用户名、字符串fName、字符串lName、字符串电子邮件、字符串imei)
{
使用(CollaborationDBEntities db=new CollaborationDBEntities())
{
UserTable userObject=new UserTable(){UserName=UserName,UserFName=fName,UserLName=lName,UserNumber=imei};
db.UserTable.Add(userObject);
}
返回null;
}
我搜索了很多,照他们说的做了,但没有任何改变。 任何帮助都非常感谢。
致以最诚挚的问候。

很可能是权限问题

1) 如果您连接到本地数据库,下面是答案

2) 如果连接到远程服务器,请检查DB server上的防火墙设置