I am trying to execute this simple code but it fails and i get System.Net.Http.HttpRequestException: An error occurred while sending the request.
The code simply declares an HttpCLient and then I use the GetStringMethod. the same code used to work. I don't know why it doesn't now.
I searched google and stackoverflow and couldn't get the answer.
Here is my .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using FetchActor.Models;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.Collections;
namespace FetchActor
{
public partial class MainPage : ContentPage
{
HttpClient connection = new HttpClient();
MovieService movieService = new MovieService();
public MainPage()
{
InitializeComponent();
}
private async void searchBarByActor_SearchButtonPressed(object sender, EventArgs e)
{
var _connection = await connection.GetStringAsync("https://api.themoviedb.org/3/search/keyword?api_key=ab3da1af35c55cc8b3780fbc9a2bf259&query=Charlize%20Theron&page=1");
}
}
}
the code never goes past GetStringAsync and i get the error.
any ideas on what could this be please ?
It seems the emulator wasn't get data from the connection. I turned the Wifi on and off and restarted the computer and the project. it still didn't work. it was showing connected but no internet.
the solution was turning off mobile data in the emulator so that it could be connected only with wifi.
Related
Having successfully created and run a simple Azure Durable Function in Visual Studio 2017, I want to introduce logging.
The Visual Studio project template generates the static HttpStart class with a
Run method containing an optional parameter of type Microsoft.Extensions.Logging.ILogger.
I have no idea how to hook up dependency injection into a durable function project. Can anyone point me to an example on how to achieve this?
It looks to me like I will need some class, inside which I will need to use the Microsoft.Extensions.Logging.LoggingFactory.CreateLogger() method.
I imagine that this logic will need to be within a container class that is hooked into the run time pipeline using HostBuilder in some way (similar to using WebHostBuilder in static main method).
Thank you
#Thomas pointed me to examples I used to extract code that allows dependency injection to work when writing a durable function project:
using System.IO;
using System.Reflection;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Extensions.Logging;
using tmetadastoreFnApp;
using Willezone.Azure.WebJobs.Extensions.DependencyInjection;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
[assembly: WebJobsStartup(typeof(Startup))]
namespace tmetadastoreFnApp
{
internal class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder) =>
builder.AddDependencyInjection(ConfigureServices);
private void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ILoggerFactory, LoggerFactory>();
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
services.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace));
var serviceProvider = services.BuildServiceProvider();
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.LoadConfiguration(Directory.GetParent(dir) + "\\nlog.config");
}
}
}
I have a WCF RESTful (i.e. JSON) service I'm building in C#. One of the DataContract methods can return a response that is very large, minimum 10 MB and maximum could be over 30 MB. It's all text and it returns it as JSON data to the client. When I test this method in the browser, I see it timing out. I understand there is a way to compress WCF RESTful service response data. Since interoperability is absolutely critical for my purposes, is it still possible to compress WCF RESTful service response data? Right now, I'm still testing the project on a local machine. I will be deploying it to IIS, however.
If there is a way to compress with interoperability, how can this be done?
Thank you.
This is not actually the set of files I'm using, but it's just a sample to show how I'm constructing my services. I realize this sample would not need compression at all.
IService1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService4
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(
Method = "GET",
UriTemplate = "employees",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
List<Employee> GetEmployees();
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Employee
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public int Age { get; set; }
public Employee(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
}
}
}
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.Text;
using System.Net;
namespace WcfService4
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public List<Employee> GetEmployees()
{
// In reality, I'm calling the data from an external datasource, returning data to the client that exceeds 10 MB and can reach an upper limit of at least 30 MB.
List<Employee> employee = new List<Employee>();
employee.Add(new Employee("John", "Smith", 28));
employee.Add(new Employee("Jane", "Fonda", 42));
employee.Add(new Employee("Brett", "Hume", 56));
return employee;
}
}
}
you can change your web.config file to solve this problem.
change httpRuntime
<httpRuntime maxRequestLength="10240" executionTimeout="1000" />
here,
maxRequestLength: Indicates the maximum file upload size supported by ASP.NET. This limit can be used to prevent denial of service attacks caused by users posting large files to the server. The size specified is in kilobytes. The default is 4096 KB (4 MB).
executionTimeout: Indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET.
I use OpenTK and MonoDevelop.
I use GLcontext
GLcontrol glControl1 = new GLControl();
I catch error:
'GLControl' does not exist in the namespace 'OpenTK'
I added OpenTK.dll, OpenTK.GLControl.dll, OpenTK.dll.config to my project.
Any Ideas.
Make sure you are using the correct libraries for your architecture. So for x86 make sure you have the x86 libraries. For x64 make sure you have the x64 libraries. Make sure you set the architecture for your startup project to the architecture to match the libraries using the configuration manager. On a 64bit machine it is usually set up by default as combile for 'Any CPU'. Change this to the correct platform.
This is the way I do it:
Create a new test windforms application. I want to do a 64bit application so I use the configuration manager to set my startup appliction to x64. Using NuGet install the opentk.glcontrol. It will automatically resolve OpenTK as a dependency and install it.
Here is some code adding the control and setting the background colour to skyblue:
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private OpenTK.GLControl _glControl;
public Form1()
{
InitializeComponent();
_glControl = new OpenTK.GLControl();
_glControl.Dock = DockStyle.Fill;
this.Controls.Add(_glControl);
_glControl.Load += control_Load;
_glControl.Paint += control_Paint;
}
private void control_Paint(object sender, PaintEventArgs e)
{
_glControl.SwapBuffers();
}
private void control_Load(object sender, EventArgs e)
{
GL.ClearColor(Color.SkyBlue);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
}
}
}
We're designing report with multilingual interface. We have developed web service which return translation of specific words. Is there any way to call the translation of labels used in report through web service or specific URL.
For e.g. something like
http://domain.com/translate?w=WORD-TO-Translate&L=ar
I was going to recommend creating a custom assembly that would do this, but while it works from Report Builder it doesn't seem to work from my SSRS server. I'm wondering if there is an issue connecting to a web service from a custom assembly (or maybe I'm doing it wrong). Instead, I'll point you to another method for doing translations.
In case you want to pick up the custom assembly approach, here's the code I'm using:
using System;
using System.IO;
using System.Net;
namespace SSRSCustomAssembly
{
public class Translate
{
public static string TranslateString(string input, string locale)
{
string url = string.Format("http://domain.com/translate?w={0}&L={1}", input, locale);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
return sr.ReadToEnd();
}
}
}
In your report, just add a reference to the assembly, the call it by having an expression:
=SSRSCustomAssembly.Translate.TranslateString("word", "en")
Given the following setup, I have three assemblies.
Web (ASP.NET MVC 3.0 RC1)
Models
Persistence (Fluent NHibernate, Castle.Windsor)
This is my ControllerInstaller.
using System;
using System.Web.Mvc;
using Castle;
using Castle.Windsor;
using Castle.MicroKernel;
using Castle.MicroKernel.SubSystems;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
namespace Persistence.Installers
{
public class ControllerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
AllTypes
.FromAssembly(System.Reflection.Assembly.GetExecutingAssembly())
.BasedOn<IController>()
.Configure(c => c.Named(
c.Implementation.Name.ToLowerInvariant()).LifeStyle.Transient));
}
}
}
This is my ControllerFactory...
using System;
using System.Web;
using System.Web.Mvc;
namespace Persistence.Containers
{
/// <summary>
/// Utilize Castle.Windsor to provide Dependency Injection for the Controller Factory
/// </summary>
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly Castle.Windsor.IWindsorContainer container;
public WindsorControllerFactory()
{
container = WindsorContainerFactory.Current();
}
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
}
}
This is my Application_Start in the global.asax file..
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
// Register the Windsor Container
ControllerBuilder.Current
.SetControllerFactory(typeof(Persistence.Containers.WindsorControllerFactory));
}
I am getting the error
No component for supporting the service Project.Web.Controllers.HomeController was found
at the GetControllerInstance.
So , I'm not really sure what I am doing wrong, and why I cannot get the Controllers registered.
Your Castle Windsor setup code all belongs in your Web project. It is nothing to do with Persistence.
This is causing the problem because your ControllerInstaller is trying to register the controllers in the Persistence assembly rather than the Web assembly with the following code:
System.Reflection.Assembly.GetExecutingAssembly().
So move the IoC code to the Web project and it will find your controllers.