我需要升级.NET应用程序以支持在仅支持TLS 1.2的网站上调用API.根据我的阅读,如果应用程序的目标是4.6或更高,那么默认情况下它将使用TLS 1.2.
为了测试我创建了一个面向4.7的Windows窗体应用程序.不幸的是,当我没有明确设置ServicePointManager.SecurityProtocol时,它会出错.这是代码:
HttpClient _client = new HttpClient();
var msg = new StringBuilder();
// If I uncomment the next line it works, but fails even with 4.7
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://sandbox.authorize.net");
httpWebRequest.KeepAlive = false;
try
{
var httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse();
msg.AppendLine("The HTTP request Headers for the first request are: ");
foreach (var header in httpWebRequest.Headers)
{
msg.AppendLine(header.ToString());
}
ResponseTextBox.Text = msg.ToString();
}
catch (Exception exception)
{
ResponseTextBox.Text = exception.Message;
if (exception.InnerException != null)
{ …Run Code Online (Sandbox Code Playgroud) 我之前一直在使用Microsoft企业库,因为它被标记为抽象数据访问,而不是编写我自己的DAL.最初我只是将一个文件(sqlhelper.cs)导入到我的项目中,但后来的版本要求我引用整个dll,除非我想在删除我想要的功能方面投入大量的工作.
我假设在.NET 4.0发布几个月后将发布新版本的Enterprise Library.我公司对库的使用可能与传统用途不同,我们为许多客户设计和开发Web和Windows应用程序.我们要么将完成的项目交给内部开发人员维护,要么是小型客户端,我们将维护应用程序.
由于业务的性质,我有幸在设计新应用程序时"从头开始"大量时间,而不是与更新相同的代码库相关联.如果我们再次使用Microsoft Enterprise Library,下一个项目我可能会问自己同样的问题吗?我们只使用数据访问块,它似乎在开发过程中节省了时间.与此同时,我想知道通过使用对象添加到项目中的开销和复杂程度是多少.
提前感谢您的建议.
这里的讨论确实让我重新考虑了这个问题 - 它可能不是关于访问存储过程的轻量级抽象,而是关于为什么我们仍然依赖于N-Tier模型的更大的架构问题.
对我来说,如果归结为应用程序中使用的数据库.在经典的3层/ N层世界中,数据库是公司信息的独立存储.不同的应用程序(Web,桌面等)都共享和访问公共存储平台.在这种情况下,存储过程是有意义的,因为它们充当各种应用程序和表之间的抽象层.
对于其他项目,数据库是较大应用程序的独占持久存储.UI或其他类型的访问(包括Web服务,远程处理等)需要通过应用程序的BLL.由于我们业务的性质,这是我们更常开发的方案.
鉴于这个结论,我将创建两个原型项目,一个使用SubSonic,另一个使用LINQ.虽然我担心LINQ的开销和丢失保真度,但是数据访问所需代码的显着减少以及我们开发的项目类型的一致性使其值得一看.
我有一个带有以下签名的控制器:
[Route("products/filter/{apc=apc}/{xpc=xpc}/{sku=sku}")]
public IHttpActionResult Get(string apc, string xpc, int? sku)
{ ... }
Run Code Online (Sandbox Code Playgroud)
我用以下URI调用此方法:
第一个URI没有问题.第二个有一个奇怪的副作用.即使apc和xpc的默认值在未提供时应为null,但参数实际上是它们的名称.我可以通过添加额外的逻辑来克服这个问题:
apc = (apc == "apc") ? null : apc;
xpc = (xpc == "xpc") ? null : xpc;
Run Code Online (Sandbox Code Playgroud)
这似乎是一个黑客攻击,如果传递的值等于参数名称,则会出现问题.
有没有办法定义路线没有这种副作用?
我有一个Windows服务,根据计划运行各种作业.确定要运行的作业后,会将一个计划对象列表发送到迭代列表并运行每个作业的方法.问题是由于外部数据库调用,某些作业最多可能需要10分钟才能运行.
我的目标是没有一个工作阻止其他队列,基本上一次有多个运行.我认为使用async和await可以解决这个问题,但我以前从未使用过这些.
现行代码:
public static bool Load(List<Schedule> scheduleList)
{
foreach (Schedule schedule in scheduleList)
{
Load(schedule.ScheduleId);
}
return true;
}
public static bool Load(int scheduleId)
{
// make database and other external resource calls
// some jobs run for up to 10 minutes
return true;
}
Run Code Online (Sandbox Code Playgroud)
我尝试更新到这段代码:
public async static Task<bool> LoadAsync(List<Schedule> scheduleList)
{
foreach (Schedule schedule in scheduleList)
{
bool result = await LoadAsync((int)schedule.JobId, schedule.ScheduleId);
}
return true;
}
public async static Task<bool> LoadAsync(int scheduleId)
{
// make database …Run Code Online (Sandbox Code Playgroud) 我的目标是在应用程序中创建一个处理所有托管错误的错误处理,而不仅仅是与MVC相关.所以我没有使用HandleErrorAttribute模式,因为它仅用于MVC错误,而不是其他托管错误.
在我的Global.asax中,我有:
protected void Application_Error()
{
string displayError = ConfigurationManager.AppSettings["DisplayError"];
NameValueCollection serverVariables;
int loop1, loop2;
StringBuilder serverVariableKeys = new StringBuilder();
Exception exception = Server.GetLastError();
HttpException httpException = exception as HttpException;
if (HttpContext.Current != null)
{
// loop through and get server vars
}
if (exception != null)
// Log Error
// Redirect to Error page if set to basic mode
if (!string.IsNullOrWhiteSpace(displayError) && displayError.ToLower() == "basic")
{
Response.Clear();
Server.ClearError(); // needed for redirect to work
var routeData = new RouteData(); …Run Code Online (Sandbox Code Playgroud) 我希望某些页面的客户端有10分钟缓存,服务器有24小时.原因是如果页面更改,客户端将在10分钟内获取更新的版本,但如果没有任何更改,服务器将只需每天重建一次页面.
问题是输出缓存设置似乎覆盖了客户端设置.这是我设置的内容:
自定义ActionFilterAttribute类
public class ClientCacheAttribute : ActionFilterAttribute
{
private bool _noClientCache;
public int ExpireMinutes { get; set; }
public ClientCacheAttribute(bool noClientCache)
{
_noClientCache = noClientCache;
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (_noClientCache || ExpireMinutes <= 0)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
}
else
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(ExpireMinutes));
}
base.OnResultExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
Web配置设置
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache24Hours" location="Server" duration="86400" varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它:
[OutputCache(CacheProfile = "Cache24Hours")]
[ClientCacheAttribute(false, ExpireMinutes = 10)]
public class HomeController : Controller
{
[...]
} …Run Code Online (Sandbox Code Playgroud) 我有以下帮助器方法,它从DbEntityValidationException中获取验证消息.我们需要这个,因为默认情况下,验证的细节不会添加到Exception中.
public static string LogMessageDbEntityValidationException(DbEntityValidationException ex)
{
StringBuilder error = new StringBuilder();
error.AppendLine("Validation Error details for DbEntityValidationException throw: ");
foreach (var validationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
error.AppendLine(string.Format("Property: {0} , Error: {1}",
validationError.PropertyName, validationError.ErrorMessage));
}
}
return error.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我遇到了一个试图创建单元测试的问题,特别是我无法创建DbEntityValidationResult,因为它需要一个DbEntityEntry实例,它没有公共构造函数.
这是单元测试中断,它在创建DbEntityEntry时失败:
public void LogMessageDbEntityValidationExceptionTest()
{
string errorMessage = "Unit Test Error Message";
string expected = "Not valid data.";
List<DbEntityValidationResult> entityValidationResults = new List<DbEntityValidationResult>();
List<DbValidationError> errorList = new List<DbValidationError>();
DbEntityValidationException ex;
errorList.Add(new DbValidationError("TestProperty", expected));
entityValidationResults.Add(new DbEntityValidationResult(new System.Data.Entity.Infrastructure.DbEntityEntry(), …Run Code Online (Sandbox Code Playgroud) 我正在寻找Windows Azure上会话状态管理的当前(2013)建议.我在网上发现了一些文章,但它们引用了较旧的神话.我以为我在这篇微软文章中找到了最新的方法,但当我进入第2步,说明在Windows Azure管理门户中设置缓存时,它不是一个选项.我假设有一种我没有看到的新方法.
这是我的要求:
基本上我正在寻找与非Azure ASP.NET部署中使用的会话状态服务或SQL会话状态选项等进程外存储提供程序等效的方法.
我必须使用缓存的另一个问题是删除会话时的情况是什么?通常我认为Cache不会因为过期而保证,并为较新的缓存项腾出空间.我想确保会话不会因为它们在Cache中而消失.
更新:
我正在开发一个带有Oracle数据库的ASP.NET项目.我们使用TOAD来添加/管理存储过程 - 通常我喜欢TOAD和Oracle.我发现令人沮丧的一件事是找到一种简单的方法来测试Oracle Stored Proc,例如SQL Server的"exec [SP_NAME] Param1,Param2,ParamN"语法.
我们所有的存储过程都输出Ref Cursors.以下是存储过程的示例:
CREATE OR REPLACE PROCEDURE APP_DB1.GET_JOB
(
p_JOB_ID IN JOB.JOB_ID%type,
outCursor OUT MYGEN.sqlcur
)
IS
BEGIN
OPEN outCursor FOR
SELECT *
FROM JOB
WHERE JOB_ID = p_JOB_ID;
END GET_JOB;
/
有什么建议?
c# ×5
.net ×4
asp.net ×4
asp.net-mvc ×2
.net-core ×1
async-await ×1
asynchronous ×1
azure ×1
caching ×1
mstest ×1
oracle ×1
outputcache ×1
tls1.2 ×1
unit-testing ×1
winforms ×1