简体   繁体   English

在Azure上部署WCF服务,尝试访问数据库时超时

[英]Deploy WCF service on Azure, timeout when try to access database

I have a first projet "MyProject.Core", I have the EDMX with : 我有第一个项目“ MyProject.Core”,有EDMX:

The repo class : 回购类:

class MyProjectRepo : IMyProjects
{
    public int NumberOfUser()
    {
        return new context().User.Count();
    }

    public string HelloWorld()
    {
        return "Hello World!";
    }
}

The interface : 界面:

public interface IMyProjects
{
    int NumberOfUser();
    string HelloWorld();
}

The factory : 该工厂 :

public static class MyProjectFactory
{
    private static IMyProjects _returnedObject;

    public static IMyProjects GetObject()
    {
        lock (typeof(MyProjectFactory))
        {
            _returnedObject = new MyProjectRepo();
        }
        return _returnedObject;
    }
}

A test project ""MyProject.Core.Tests" (tests passed) : 一个测试项目““ MyProject.Core.Tests”(测试通过):

[Test]
public void NumberOfUser_Test()
{
    var number = MyProjectFactory.GetObject().NumberOfUser();
    Assert.AreEqual(1, number);
}

[Test]
public void HelloWorld_Test()
{
    var hello = MyProjectFactory.GetObject().HelloWorld();
    Assert.AreEqual("Hello World!", hello);
}

I created a "Cloud" project and a WCFServiceWebRole. 我创建了一个“云”项目和一个WCFServiceWebRole。

In the WCFServiceWebRole, I have this : 在WCFServiceWebRole中,我有这个:

public class Service1 : IService1
{
    public int NumberOfUser()
    {
        return MyProjectFactory.GetObject().NumberOfUser();
    }

    public string Hello()
    {
        return MyProjectFactory.GetObject().HelloWorld(); 
    }
}

[ServiceContract]
public interface IService1
{
    [OperationContract]
    int NumberOfUser(string login, string password);

    [OperationContract]
    string Hello();
}

A project to test the WCF, the methode "Hello" return the right value. 一个测试WCF的项目,方法“ Hello”返回正确的值。 It's with the other method I have thr problem. 这是我遇到问题的另一种方法。 In the app.config, I have this : 在app.config中,我有这个:

<bindings>
    <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" />
    </basicHttpBinding>
</bindings>

<client>
    <endpoint address="http://myproject.azurewebsites.net/Service1.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
        contract="myprojectServiceAzure.IService1" name="BasicHttpBinding_IService1" />
</client>

Error : 错误:

System.TimeoutException : The request channel timed out while waiting for a reply after 00:00:59.7138476. System.TimeoutException:请求通道在00:00:59.7138476之后等待答复时超时。 Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. 增加传递给请求的调用的超时值,或者增加绑定上的SendTimeout值。 The time allotted to this operation may have been a portion of a longer timeout. 分配给该操作的时间可能是较长超时的一部分。 ----> System.TimeoutException : The HTTP request to 'http://MyProject.azurewebsites.net/Service1.svc' has exceeded the allotted timeout of 00:00:59.9270000. ----> System.TimeoutException:对“ http://MyProject.azurewebsites.net/Service1.svc”的HTTP请求已超过分配的超时时间00:00:59.9270000。 The time allotted to this operation may have been a portion of a longer timeout. 分配给该操作的时间可能是较长超时的一部分。 ----> System.Net.WebException : The operation has timed out MyProject.Azure.Tests\\Service References\\MyProjectServiceAzure\\Reference.cs(487, 0) : MyProject.Azure.Tests.PointageServiceAzure.Service1Client.MyMethod(String login, String password) MyProject.Azure.Tests\\AzureTests.cs(18, 0) : MyProject.Azure.Tests.AzureTests.Test() ----> System.Net.WebException:操作已超时MyProject.Azure.Tests \\ Service References \\ MyProjectServiceAzure \\ Reference.cs(487,0):MyProject.Azure.Tests.PointageServiceAzure.Service1Client.MyMethod(字符串登录,字符串密码)MyProject.Azure.Tests \\ AzureTests.cs(18,0):MyProject.Azure.Tests.AzureTests.Test()

Have you checked the Azure firewall rules? 您是否检查过Azure防火墙规则? You need to 'allow' the WCF service access to the SQL Azure database. 您需要“允许” WCF服务访问SQL Azure数据库。 From memory, there's a checkbox that you need to check, which allows the hosted application access to the database, as this is not checked by default. 在内存中,需要选中一个复选框,该复选框允许托管应用程序访问数据库,因为默认情况下未选中该复选框。

Did you try to set your MaxReceivedMessageSize etc in your web.config ? 您是否尝试在web.config中设置MaxReceivedMessageSize等?

See Here 这里

It's not an answer to your question. 这不是您问题的答案。 It's a solution of the real problem you're facing :) 这是您面临的实际问题的解决方案:)

You're not really writing a unit test, but an integration test. 您并不是真正在编写单元测试,而是在编写集成测试。

You must ask yourself what do you really want to test. 您必须问自己,自己真正想要测试什么。 As far as I can see, you're testing connection to your WCF service, and that's not what unit tests are designed for. 据我所知,您正在测试与WCF服务的连接,而这并不是单元测试的目的。

What I suggest is a separation of concerns. 我建议将关注点分离。 WCF is the only proxy that allows remote communication. WCF是唯一允许远程通信的代理。 Therefore it's not part of core business logic. 因此,它不是核心业务逻辑的一部分。 Your business logic should be as pure as possible. 您的业​​务逻辑应尽可能纯净。 No unnecessary dependencies which might be hard to get rid of. 没有不必要的依赖关系,这些依赖关系可能很难摆脱。 Should you have to replace WCF with carrier pigeons ( ;) ), with logic unaware of WCF you don't have to touch logic code at all. 如果您必须用信鸽(;))替换WCF,而逻辑上不知道WCF,则根本不需要接触逻辑代码。

Here's an example of my idea: 这是我的想法的一个例子:

interface IService //pure business logic, no knowledge of WCF
{
    double Add(double a, double b);
}

class NonWcfService : IService //pure business logic
{
    public double Add(double a, double b)
    {
        return a + b;
    }
}

[ServiceContract]
interface IServiceContract //wcf-bound (because of the attributes)
{
    [OperationContract]
    double Add(double a, double b);
}

class WcfService : IServiceContract //wcf-bound delegates calls to pure business logic
{
    IService sourceService;

    public WcfService(IService sourceService)
    {
        this.sourceService = sourceService; 
    }

    public double Add(double a, double b)
    {
        return sourceService.Add(a,b);
    }
}

Now you can write unit tests for NonWcfService without having to connect to any service. 现在,您无需连接任何服务即可编写NonWcfService的单元测试。 It implies that your unit tests would run much faster. 这意味着您的单元测试将运行得更快。

You may also write unit tests for WcfService that would check if this wrapper behaves well as a proxy. 您也可以为WcfService编写单元测试,以检查此包装器是否作为代理运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM