ASP.NET MVC网站从磁盘问题中读取文件

我在正在研究的MVC网站上正在使用C#读取包含SQL插入语句的文本文件。 调试我正在使用的函数工作正常,插入发生。 但是一旦我发布网站并在我的本地计算机上运行它(使用IIS设置甚至使用asp.net 4.0)它似乎不起作用。 if (Request.Files != null && Request.Files[“txtImportFile”] != null) { //newFilePath = Server.MapPath(“\\” + DateTime.Now.Ticks + Request.Files[“txtImportFile”].FileName); string[] temp_string = Request.Files[“txtImportFile”].FileName.Split(new char[] { ‘\\’ }); string temp_filename = temp_string[temp_string.Count() – 1]; //newFilePath = Server.MapPath(“\\temp\\” + DateTime.Now.Ticks + Request.Files[“txtImportFile”].FileName); newFilePath = Server.MapPath(“\\temp\\” + DateTime.Now.Ticks + temp_filename); Request.Files[“txtImportFile”].SaveAs(newFilePath); StreamReader reader = new StreamReader(newFilePath); string […]

async / await返回callchain是如何工作的?

我最近有一种情况,我有一个ASP.NET WebAPI控制器,需要在其action方法中对另一个REST服务执行两个Web请求。 我编写了我的代码,将function完全分离为单独的方法,看起来有点像这个例子: public class FooController : ApiController { public IHttpActionResult Post(string value) { var results = PerformWebRequests(); // Do something else here… } private IEnumerable PerformWebRequests() { var result1 = PerformWebRequest(“service1/api/foo”); var result = PerformWebRequest(“service2/api/foo”); return new string[] { result1, result2 }; } private string PerformWebRequest(string api) { using (HttpClient client = new HttpClient()) […]

我可以将自定义版本字符串添加到.net DLL吗?

我可以通过手动编辑.rc文件将自定义版本字符串添加到Visual Studio中的C ++ DLL。 例如,如果我添加到.rc文件的VersionInfo部分 VALUE “BuildDate”, “2008/09/19 15:42:52” 然后,该日期在文件资源管理器中,在DLL的属性中,在“版本”选项卡下可见。 我可以为C#DLL做同样的事情吗? 不仅适用于构建日期,还适用于其他版本信息(例如源控制信息) 更新:我认为可能有一种方法可以通过嵌入Windows资源来实现这一点,所以我已经问过如何做到这一点 。

使用RestSharp为Etsy的API获取signature_invalid调用oauth / request_token

我正在尝试使用RestSharp来访问Etsy的API。 以下是我尝试获取OAuth访问令牌的代码: var authenticator = OAuth1Authenticator.ForRequestToken( ConfigurationManager.AppSettings[“ApiKey”], ConfigurationManager.AppSettings[“ApiSecret”]); // same result with or without this next line: // authenticator.ParameterHandling = OAuthParameterHandling.UrlOrPostParameters; this.Client.Authenticator = authenticator; var request = new RestRequest(“oauth/request_token”) .AddParameter(“scope”, “listings_r”); var response = this.Client.Execute(request); Etsy告诉我签名无效。 有趣的是,当我将请求生成的时间戳和现时值输入到此OAuth签名validation工具中时 ,签名不匹配。 此外,该工具生成的URL与Etsy一起使用,其中RestSharp生成的URL不支持。 有什么我做错了或我需要用RestSharp配置的其他东西? 注意:我正在使用他们的Nuget包提供的RestSharp版本,该版本在发布时为102.5。

使用Moq测试抽象类

我正在尝试对抽象类中的方法运行unit testing。 我浓缩了下面的代码: 抽象类: public abstract class TestAb { public void Print() { Console.WriteLine(“method has been called”); } } 测试: [Test] void Test() { var mock = new Mock(); mock.CallBase = true; var ta = mock.Object; ta.Print(); mock.Verify(m => m.Print()); } 信息: Method is not public 我在这做错了什么? 我的目标是使用Moq框架测试抽象类中的方法。

如何设计一个类来接收具有未知数量参数的委托?

我不断发现自己必须编写定时窗口服务,在队列外部轮询或运行定时进程。 因此,我有一个相当强大的模板来执行此操作,但我发现每次编写服务来执行此操作时,我都会使用相同的模板,然后在其中编写进程。 今天早上我发现自己想知道我是否真的可以将这个模板变成一个框架,我可以将我的进程注入其中。 我的基本模板只有122行代码。 由于每个进程的要求不同 – 即不同的参数数量,不同的参数类型和不同的依赖关系(一些依赖于Web服务,一些依赖于数据库等)我无法弄清楚如何设置我的基本模板来接收一个注入过程。 模板的核心只是一个计时器,它在初始化并启动进程时停止,然后在进程完成后重新启动计时器。 然后,我将我的流程方法和任何依赖项添加到模板中。 有没有人有任何想法如何做到这一点? 我已经看过dependency injection并经常使用它来注入数据存储连接等东西? 有没有办法将具有未知数量/类型参数的委托注入类中? 我不正确地看着这个吗? 这是我的模板: TimedProcess.Template.cs using System; using System.Timers; public partial class TimedProcess : IDisposable { private Timer timer; public bool InProcess { get; protected set; } public bool Running { get { if (timer == null) return false; return timer.Enabled; } } private […]

忽略错误的证书 – .NET CORE

我正在编写一个.NET Core应用程序来轮询远程服务器并在显示时传输数据。 这在PHP中运行得很好,因为PHP忽略了证书(这也是浏览器中的一个问题),但是我们想把它转移到C#.NET CORE,因为这是系统中唯一剩下的PHP。 我们知道服务器很好,但由于各种原因,证书不能/不会很快更新。 请求是使用HttpClient: HttpClient httpClient = new HttpClient(); try { string url = “https://URLGoesHere.php”; MyData md = new MyData(); // this is some data we need to pass as a json string postBody = JsonConvert.SerializeObject(md); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); HttpResponseMessage wcfResponse = await httpClient.PostAsync(url, new StringContent(postBody, Encoding.UTF8, “application/json”)); Console.WriteLine(wcfResponse.Content); } catch (HttpRequestException hre) { […]

无法将图像渲染到HttpContext.Response.OutputStream

基本上我试图在ASP.NET处理程序中呈现一个简单的图像: public void ProcessRequest (HttpContext context) { Bitmap image = new Bitmap(16, 16); Graphics graph = Graphics.FromImage(image); graph.FillEllipse(Brushes.Green, 0, 0, 16, 16); context.Response.ContentType = “image/png”; image.Save(context.Response.OutputStream, ImageFormat.Png); } 但我得到以下exception: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) 解决方案是使用它而不是将图像写入OutputStream: MemoryStream temp = new MemoryStream(); image.Save(temp, ImageFormat.Png); byte[] buffer = temp.GetBuffer(); […]

使用Httpclient信任自签名证书

我正在尝试通过自签名证书发出失败的Web请求: Client = new HttpClient(); HttpResponseMessage Response = await Client.GetAsync(Uri)//defined elsewhere 这会引发信任失败exception。 我按照这里建议使用httpclienthandler再次尝试使用HttpClient允许不受信任的SSL证书 : var handler = new HttpClientHandler(); handler.ServerCertificateCustomValidationCallback = ( HttpRequestMessage message, X509Certificate2 cert, X509Chain chain, SslPolicyErrors errors ) =>{return true; };//remove if this makes it to production Client = new HttpClient(handler); 抛出一个未实现exception的系统。 有没有其他方法可以信任自签名证书? 我甚至在提出请求的机器上安装了证书,但没有运气。

如何以文化感知的方式格式化TimeSpan的HH:mm:ss分隔符?

我正在开发一个可以在世界上许多国家看到的应用程序。 没有多少国家/地区显示小时,分钟和秒钟以外的其他内容:作为分隔符,但有一些,我想确保时区格式正确的区域。 DateTime很棒,但TimeSpan不是。 这些片段来自我在Visual Studio 2010中的即时窗口,使用.Net 4,我的区域设置为Malayalam(印度)。 dateTime.Now调用还反映了我的时钟,Microsoft Outlook和其他领域的时间显示方式。 DateTime.Now.ToString() “02-10-12 17.00.58” http://msdn.microsoft.com/en-us/library/dd784379.aspx表示“如果formatProvider为null,则使用与当前区域性关联的DateTimeFormatInfo对象。如果format是自定义格式字符串,则为formatProvider参数被忽略了。“ 按理说那我甚至不需要传递当前的CultureInfo。 我想要的格式是hh.mm.ss但是显而易见hh:mm:ss在大多数其他语言中,如果还有其他可能性,它应该自动反映这些 – 基本上TimeSpan 应该是文化意识,就像DateTime一样是。 然而: timeRemaining.ToString() “00:02:09” timeRemaining.ToString(“c”) “00:02:09” timeRemaining.ToString(“c”, CultureInfo.CurrentCulture) “00:02:09” timeRemaining.ToString(“g”) “0:02:09” timeRemaining.ToString(“G”) “0:00:02:09.0000000” timeRemaining.ToString(“t”) “00:02:09” timeRemaining.ToString(“g”, CultureInfo.CurrentCulture) “0:02:09” timeRemaining.ToString(“g”, CultureInfo.CurrentUICulture) “0:02:09” timeRemaining.ToString(“G”, CultureInfo.CurrentUICulture) “0:00:02:09.0000000” timeRemaining.ToString(“G”, CultureInfo.CurrentCulture) “0:00:02:09.0000000” timeRemaining.ToString(“t”, CultureInfo.CurrentCulture) “00:02:09” 我正在寻找一个简单的单行来以文化意识的方式输出timeSpan。 任何想法都表示赞赏。